aboutsummaryrefslogtreecommitdiff
path: root/src/utils/mod.rs
Commit message (Collapse)AuthorAgeFilesLines
* Add Function to neutralise Mentions (#414)Lakelezz2018-10-301-3/+529
|
* Fix NSFW Checks (#418)Lakelezz2018-10-201-0/+2
|
* Generalise mention parsingacdenisSK2018-09-221-0/+25
| | | | Fixes #396
* Fix all the dead links in the docsErk-2018-08-091-1/+1
|
* Move unit tests into sourceZeyla Hellyer2018-08-011-0/+51
| | | | | | | | | Move the unit tests into the relevant source files. There's no need for them to be seprate, especially when the `tests` directory is meant to be for integration tests. The deserialization tests that include JSON files are still in the `tests` dir, along with the public prelude re-export tests.
* Monomorphize all functionsZeyla Hellyer2018-07-041-1/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | This commit monomorphizes all functions, turning functions like: ```rust fn foo<T: Into<Bar>>(baz: T) { baz = baz.into(); // function here } ``` Into functions like: ```rust fn foo<T: Into<Bar>>(baz: T) { _foo(baz.into()) } fn _foo(baz: Bar) { // function here } ``` This avoids binary bloat and improves build times, by reducing the amount of code duplication.
* properly fix the is_nsfw check this timeacdenisSK2018-07-021-1/+1
|
* Simplify utils::is_nsfwZeyla Hellyer2018-07-021-9/+1
|
* Fix 'nsfw-' case in utils::is_nsfwZeyla Hellyer2018-07-021-1/+1
| | | | | | In the utils::is_nsfw function, an input of 'nsfw-' shouldn't return true. Discord's (previous) NSFW detection classified 'nsfw' and anything starting with 'nsfw-' - but not including - as NSFW.
* Fix utils::is_nsfw string slicingZeyla Hellyer2018-07-021-4/+6
| | | | | | | | | Removes string slicing on the input string, instead preferring to count chars and work off that information. Fixes channel names with unicode such as `général`. Fixes #342.
* Fix typo'd word `retrieve` (#307)Lakelezz2018-04-141-4/+4
|
* Fix nsfw related docs (#299)Lakelezz2018-04-041-4/+0
|
* Refactor imports/exports to use nested groups and better formattingacdenisSK2018-03-291-9/+13
|
* Change the way ids and some enums are made (#295)Leah2018-03-231-1/+1
| | | | | This makes them easier to be found by tools like rls. Also update struct inits to use the shorthand version for `x: x`.
* Fix broken docs links caused by model mod changesZeyla Hellyer2018-01-311-5/+5
| | | | | Fix broken links caused by the `model` module changes in v0.5.0, which split up the module into sub-modules for better organization.
* Move `VecMap` to `utils`acdenisSK2018-01-021-1/+2
| | | | This also fixes no-builder compilation
* Remove builder re-export in utilsZeyla Hellyer2018-01-021-4/+0
|
* Improve performance of builders even furtheracdenisSK2017-12-271-0/+12
| | | | | | By negating hashing altogether. The increase is around 1000-ish nanoseconds saved.
* Fix most clippy lints, take more refeerncesZeyla Hellyer2017-12-161-4/+4
| | | | | Fix clippy lints and subsequently accept references for more function parameters.
* Break up the model moduleZeyla Hellyer2017-12-161-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `model` module has historically been one giant module re-exporting all of the model types, which is somewhere around 100 types. This can be a lot to look at for a new user and somewhat overwhelming, especially with a large number of fine-grained imports from the module. The module is now neatly split up into submodules, mostly like it has been internally since the early versions of the library. The submodules are: - application - channel - error - event - gateway - guild - id - invite - misc - permissions - prelude - user - voice - webhook Each submodule contains types that are "owned" by the module. For example, the `guild` submodule contains, but not limited to, Emoji, AuditLogsEntry, Role, and Member. `channel` contains, but not limited to, Attachment, Embed, Message, and Reaction. Upgrade path: Instead of glob importing the models via `use serenity::model::*;`, instead glob import via the prelude: ```rust use serenity::model::prelude::*; ``` Instead of importing from the root model module: ```rust use serenity::model::{Guild, Message, OnlineStatus, Role, User}; ``` instead import from the submodules like so: ```rust use serenity::model::channel::Message; use serenity::model::guild::{Guild, Role}; use serenity::model::user::{OnlineStatus, User}; ```
* Re-order use statements alphabeticallyZeyla Hellyer2017-11-111-1/+0
|
* Slightly improve performance of buildersZeyla Hellyer2017-10-181-2/+16
| | | | | | | | | | | | | | | Builders would keep a `serde_json::Map<String, Value>`, which would require re-creating owned strings for the same parameter multiple times in some cases, depending on builder defaults and keying strategies. This commit uses a `std::collections::HashMap<&'static str, Value>` internally, and moves over values to a `serde_json::Map<String, Value>` when it comes time to sending them to the appropriate `http` module function. This saves the number of heap-allocated string creations on most builders, with specific performance increase on `builder::CreateMessage` and `builder::CreateEmbed` & co.
* Switch to parking_lot::{Mutex, RwLock}Zeyla Hellyer2017-10-101-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Switch to the `parking_lot` crate's implementations of `std::sync::Mutex` and `std::sync::RwLock`, which are more efficient. A writeup on why `parking_lot` is more efficient can be read here: <https://github.com/Amanieu/parking_lot> Upgrade path: Modify `mutex.lock().unwrap()` usage to `mutex.lock()` (not needing to unwrap or handle a result), and `rwlock.read().unwrap()`/`rwlock.write().unwrap()` usage to `rwlock.read()` and `rwlock.write()`. For example, modify: ```rust use serenity::CACHE; println!("{}", CACHE.read().unwrap().user.id); ``` to: ```rust use serenity::CACHE; println!("{}", CACHE.read().user.id); ```
* `to_owned` -> `to_string`acdenisSK2017-10-091-4/+4
|
* Fix compiles of a variety of feature combinationsZeyla Hellyer2017-09-181-0/+5
| | | | | This fixes compilation errors and warnings when compiling a mixture of non-default feature targets.
* Apply rustfmt fixesZeyla Hellyer2017-08-221-6/+8
|
* Add `with_config{_mut}`acdenisSK2017-08-231-0/+40
|
* Change the config a bit, and a few nitpicksacdenisSK2017-07-271-6/+4
|
* rustfmtacdenisSK2017-07-271-12/+12
|
* Add Content for MessageBuilderSkye2017-05-301-2/+1
| | | Allow `push` and `push_safe` to use a flexible syntax for formatting.
* Add more examples and improve some othersZeyla Hellyer2017-05-231-11/+143
| | | | | Add examples to some functions, and update some of the old examples to use the `?` operator instead of unwrapping.
* Restructure modulesZeyla Hellyer2017-05-221-5/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Modules are now separated into a fashion where the library can be used for most use cases, without needing to compile the rest. The core of serenity, with no features enabled, contains only the struct (model) definitions, constants, and prelude. Models do not have most functions compiled in, as that is separated into the `model` feature. The `client` module has been split into 3 modules: `client`, `gateway`, and `http`. `http` contains functions to interact with the REST API. `gateway` contains the Shard to interact with the gateway, requiring `http` for retrieving the gateway URL. `client` requires both of the other features and acts as an abstracted interface over both the gateway and REST APIs, handling the event loop. The `builder` module has been separated from `utils`, and can now be optionally compiled in. It and the `http` feature are required by the `model` feature due to a large number of methods requiring access to them. `utils` now contains a number of utilities, such as the Colour struct, the `MessageBuilder`, and mention parsing functions. Each of the original `ext` modules are still featured, with `cache` not requiring any feature to be enabled, `framework` requiring the `client`, `model`, and `utils`, and `voice` requiring `gateway`. In total the features and their requirements are: - `builder`: none - `cache`: none - `client`: `gateway`, `http` - `framework`: `client`, `model`, `utils` - `gateway`: `http` - `http`: none - `model`: `builder`, `http` - `utils`: none - `voice`: `gateway` The default features are `builder`, `cache`, `client`, `framework`, `gateway`, `model`, `http`, and `utils`. To help with forwards compatibility, modules have been re-exported from their original locations.
* Add `is_nsfw` check to channelsZeyla Hellyer2017-04-261-0/+53
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A new feature in Discord is warning users about NSFW channels. This can be useful when wanting to determine if a command can be used in a channel or not, depending on the command content. To help with this, provide a utility function named `utils::is_nsfw`. This function accepts a channel name, which determines if the channel is NSFW. This information is not provided with data from the server. It is determined client-side based on a few rules. The rules for a channel being NSFW are: - must be a guild channel - must be a text channel - must be named `nsfw` or be prefixed with `nsfw-` If any of these conditions are false, then the channel is not NSFW. Additionally, provide four helper methods: - `GuildChannel::is_nsfw`: follows rules - `Group::is_nsfw`: always false - `PrivateChannel::is_nsfw`: always false - `Channel::is_nsfw`: depends on inner channel (one of 3 above) This check is volatile, as Discord may change requirements at any time. The check provided by the library should not be taken as being accurate all the time.
* Add Shard Id helpersZeyla Hellyer2017-04-121-0/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add helpers to retrieve the shard Id for guilds, and count how many guilds are handled by a Shard. Helpers to retrieve the shard Id of a guild have been added as: - `Guild::shard_id` - `GuildId::shard_id` These are in two forms: one working with the cache feature, and one without. The function that works with the cache will automatically retrieve the total number of shards from the Cache, while the uncached version requires passing in the total number of shards used. With the cache enabled, this might look like: ```rust guild.shard_id(); // which calls: guild_id.shard_id(); ``` Without the cache enabled, this looks like: ```rust let shard_count = 7; guild.shard_id(shard_count); // which calls: guild_id.shard_id(shard_count); ``` These two variants on `Guild` and `GuildId` are helper sugar methods over the new function `utils::shard_id`, which accepts a `guild_id` and a `shard_count`: ```rust use serenity::utils; assert_eq!(utils::shard_id(81384788765712384, 17), 7); ``` You would use `utils::shard_id` when you have the total number of shards due to `{Guild,GuildId}::shard_id` unlocking the cache to retrieve the total number of shards. This avoids some amount of work
* Switch to using serde for deserializationZeyla Hellyer2017-04-111-13/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The current build system is rudimentary, incomplete, and rigid, offering little in the way of customizing decoding options. To solve this, switch to using serde-derive with custom Deserialization implementations. This allows very simple deserialization when special logic does not need to be applied, yet allows us to implement our own deserialization logic when required. The problem with the build system was that it built enums and structs from YAML files. This is not so good, because it requires creating a custom build system (which was rudimentary), creating "special struct configs" when logic needed to be ever so slightly extended (rigid), and if special logic needed to be applied, a custom deserialization method would have been needed to be made anyway (incomplete). To solve this, switch to serde-derive and implementing Deserialize ourselves where required. This reduces YAML definitions that might look like: ```yaml --- name: Group description: > A group channel, potentially including other users, separate from a [`Guild`]. [`Guild`]: struct.Guild.html fields: - name: channel_id description: The Id of the group channel. from: id type: ChannelId - name: icon description: The optional icon of the group channel. optional: true type: string - name: last_message_id description: The Id of the last message sent. optional: true type: MessageId - name: last_pin_timestamp description: Timestamp of the latest pinned message. optional: true type: string - name: name description: The name of the group channel. optional: true type: string - name: owner_id description: The Id of the group channel creator. type: UserId - name: recipients description: Group channel's members. custom: decode_users t: UserId, Arc<RwLock<User>> type: hashmap ``` to: ```rs /// A group channel - potentially including other [`User`]s - separate from a /// [`Guild`]. /// /// [`Guild`]: struct.Guild.html /// [`User`]: struct.User.html pub struct Group { /// The Id of the group channel. #[serde(rename="id")] pub channel_id: ChannelId, /// The optional icon of the group channel. pub icon: Option<String>, /// The Id of the last message sent. pub last_message_id: Option<MessageId>, /// Timestamp of the latest pinned message. pub last_pin_timestamp: Option<String>, /// The name of the group channel. pub name: Option<String>, /// The Id of the group owner. pub owner_id: UserId, /// A map of the group's recipients. #[serde(deserialize_with="deserialize_users")] pub recipients: HashMap<UserId, Arc<RwLock<User>>>, } ``` This is much simpler and does not have as much boilerplate. There should not be any backwards incompatible changes other than the old, public - yet undocumented (and hidden from documentation) - decode methods being removed. Due to the nature of this commit, field names may be incorrect, and will need to be corrected as deserialization errors are found.
* allow quotes to be escaped in quote parserIllia2017-01-151-1/+5
|
* Abstract command fields to typesAustin Hellyer2016-12-091-15/+13
|
* Command builder, quoted args, and multi-prefixesIllia2016-12-091-0/+46
| | | | | Add a command builder, which can take arguments such as multiple checks, quoted arguments, and multiple prefix support, as well as dynamic prefixes per context.
* Change all try's into ?sacdenisSK2016-12-071-1/+1
| | | This breaks compatibility with < 1.13, but we didn't support that anyway.
* Allow mentionable structs to be used as command argumentsIllia2016-12-071-0/+83
| | | | | Add EmojiIdentifier, allow User, UserId, Role, RoleId, EmojiIdentifier, Channel and ChannelId to be used as arguments for commands and add more parsing functions to utils
* Clean up the codebaseAustin Hellyer2016-11-291-54/+0
|
* Improve docs and add new message builder methodsIllia K2016-11-281-8/+2
| | | | | Add documentation for some missing methods - such as Game methods - and add more methods to the Message Builder.
* Remove the 'extras' feature flagAustin Hellyer2016-11-261-2/+0
| | | | | | There aren't many things behind this flag (6), and it only causes annoyances for locally-generated docs, which won't show these mostly-useful items behind the flag.
* A bit of docsAustin Hellyer2016-11-181-4/+27
|
* Add state/framework/etc. conditional compile flagsAustin Hellyer2016-11-151-79/+11
| | | | | | | | | | | | | | | This adds conditional compilation for the following features, in addition to the voice conditional compilation flag: - extras (message builder) - framework - methods - state These 4 are enabled _by default_, while the `voice` feature flag is disabled. Disabling the state will allow incredibly low-memory bots.
* Add voice connection supportAustin Hellyer2016-11-141-1/+50
|
* Add internal moduleAustin Hellyer2016-11-141-1/+1
| | | | | Create a general `internal` module, and move `prelude_internal` to `internal::prelude`.
* Move the builders to the utilsAustin Hellyer2016-11-131-0/+2
| | | | | | | | | The builders aren't a large enough portion of the library to deserve their own root-level module, so move them to the `utils` module. Additionally, split them into separate files, as the library will be receiving more builders and the single-file pattern was getting rather large.
* Fix some clippy lintsAustin Hellyer2016-11-101-0/+3
|
* Map op codes via a macroAustin Hellyer2016-11-091-0/+26
|