diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/channel/group.rs | 13 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 16 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 14 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 13 | ||||
| -rw-r--r-- | src/utils/mod.rs | 53 |
5 files changed, 109 insertions, 0 deletions
diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index b85bc7d..bf87590 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -149,6 +149,19 @@ impl Group { format!(cdn!("/channel-icons/{}/{}.webp"), self.channel_id, icon)) } + /// Determines if the channel is NSFW. + /// + /// Refer to [`utils::is_nsfw`] for more details. + /// + /// **Note**: This method is for consistency. This will always return + /// `false`, due to groups not being considered NSFW. + /// + /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html + #[inline] + pub fn is_nsfw(&self) -> bool { + false + } + /// Leaves the group. #[inline] pub fn leave(&self) -> Result<Group> { diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 6cbdda0..d8fa5c2 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -5,6 +5,7 @@ use ::client::rest; use ::internal::prelude::*; use ::model::*; use ::utils::builder::{CreateInvite, CreateMessage, EditChannel, GetMessages}; +use ::utils as serenity_utils; #[cfg(feature="cache")] use ::client::CACHE; @@ -319,6 +320,21 @@ impl GuildChannel { self.id.invites() } + /// Determines if the channel is NSFW. + /// + /// Refer to [`utils::is_nsfw`] for more details. + /// + /// Only [text channels][`ChannelType::Text`] are taken into consideration + /// as being NSFW. [voice channels][`ChannelType::Voice`] are never NSFW. + /// + /// [`ChannelType::Text`]: enum.ChannelType.html#variant.Text + /// [`ChannelType::Voice`]: enum.ChannelType.html#variant.Voice + /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html + #[inline] + pub fn is_nsfw(&self) -> bool { + self.kind == ChannelType::Text && serenity_utils::is_nsfw(&self.name) + } + /// Gets a message from the channel. /// /// Requires the [Read Message History] permission. diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 09cf70a..a54acdf 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -165,6 +165,20 @@ impl Channel { self.id().edit_message(message_id, f) } + /// Determines if the channel is NSFW. + /// + /// Refer to [`utils::is_nsfw`] for more details. + /// + /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html + #[inline] + pub fn is_nsfw(&self) -> bool { + match *self { + Channel::Group(_) => false, + Channel::Guild(ref channel) => channel.read().unwrap().is_nsfw(), + Channel::Private(_) => false, + } + } + /// Gets a message from the channel. /// /// Requires the [Read Message History] permission. diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index 2ede408..910daa2 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -128,6 +128,19 @@ impl PrivateChannel { self.id.edit_message(message_id, f) } + /// Determines if the channel is NSFW. + /// + /// Refer to [`utils::is_nsfw`] for more details. + /// + /// **Note**: This method is for consistency. This will always return + /// `false`, due to DMs not being considered NSFW. + /// + /// [`utils::is_nsfw`]: ../utils/fn.is_nsfw.html + #[inline] + pub fn is_nsfw(&self) -> bool { + false + } + /// Gets a message from the channel. /// /// Requires the [Read Message History] permission. 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 |