diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-26 21:27:16 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-26 21:27:16 -0700 |
| commit | 9268f9c10ef47ffeaeb3d5040e65b1093e04b866 (patch) | |
| tree | 94f8b0a8d38b2f91206ee2010ee110c3b1ce76dd /src/utils | |
| parent | Make `User.discriminator` a u16 (diff) | |
| download | serenity-9268f9c10ef47ffeaeb3d5040e65b1093e04b866.tar.xz serenity-9268f9c10ef47ffeaeb3d5040e65b1093e04b866.zip | |
Add `is_nsfw` check to channels
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.
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/mod.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index adb2d45..ec19ff7 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -22,6 +22,59 @@ use ::model::{EmojiIdentifier, EmojiId}; pub use self::message_builder::MessageBuilder; +/// Determines if a name is NSFW. +/// +/// This checks that the name is either `"nsfw"` or, for names longer than that, +/// is prefixed with `"nsfw"`. +/// +/// **Note**: Whether a channel is NSFW is done client-side, as a field for the +/// NSFW-ness of a channel is not sent to clients. Discord's requirements for +/// defining a channel as NSFW can change at any time. +/// +/// # Examples +/// +/// Check that a channel named `"nsfw"` is in fact NSFW: +/// +/// ```rust +/// use serenity::utils; +/// +/// assert!(utils::is_nsfw("nsfw")); +/// ``` +/// +/// Check that a channel named `"cats"` is _not_ NSFW: +/// +/// ```rust +/// use serenity::utils; +/// +/// assert!(!utils::is_nsfw("cats")); +/// ``` +/// +/// Check that a channel named `"nsfw-stuff"` _is_ NSFW: +/// +/// ```rust +/// use serenity::utils; +/// +/// assert!(utils::is_nsfw("nsfw-stuff")); +/// ``` +/// +/// Channels prefixed with `"nsfw"` but not the hyphen (`'-'`) are _not_ +/// considered NSFW: +/// +/// ```rust +/// use serenity::utils; +/// +/// assert!(!utils::is_nsfw("nsfwstuff")); +/// ``` +pub fn is_nsfw(name: &str) -> bool { + if name.len() == 4 { + &name[..4] == "nsfw" + } else if name.len() > 4 { + &name[..5] == "nsfw-" + } else { + false + } +} + /// Retrieves the "code" part of an [invite][`RichInvite`] out of a URL. /// /// # Examples |