diff options
| author | Mishio595 <[email protected]> | 2018-11-13 22:23:39 -0700 |
|---|---|---|
| committer | Mishio595 <[email protected]> | 2018-11-13 22:23:39 -0700 |
| commit | 1dad6996d8e9c983dc9e1689a93dbef5b0696c22 (patch) | |
| tree | 1ee7226178cae76b20e6adcdc7e7cfcc3c477c32 /src/model | |
| parent | Finish rebase (diff) | |
| parent | Make `Region`s `Japan`-variant lowercase. (diff) | |
| download | serenity-1dad6996d8e9c983dc9e1689a93dbef5b0696c22.tar.xz serenity-1dad6996d8e9c983dc9e1689a93dbef5b0696c22.zip | |
Merge branch 'upstream'
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel/channel_category.rs | 3 | ||||
| -rw-r--r-- | src/model/channel/channel_id.rs | 14 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 4 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 6 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 9 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 10 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 4 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 2 | ||||
| -rw-r--r-- | src/model/guild/role.rs | 9 | ||||
| -rw-r--r-- | src/model/invite.rs | 4 | ||||
| -rw-r--r-- | src/model/misc.rs | 2 | ||||
| -rw-r--r-- | src/model/permissions.rs | 2 | ||||
| -rw-r--r-- | src/model/user.rs | 25 |
13 files changed, 62 insertions, 32 deletions
diff --git a/src/model/channel/channel_category.rs b/src/model/channel/channel_category.rs index 47b2281..f57910e 100644 --- a/src/model/channel/channel_category.rs +++ b/src/model/channel/channel_category.rs @@ -125,10 +125,9 @@ impl ChannelCategory { }) } - #[cfg(feature = "utils")] #[inline] pub fn is_nsfw(&self) -> bool { - self.kind == ChannelType::Text && (self.nsfw || serenity_utils::is_nsfw(&self.name)) + self.kind == ChannelType::Text && self.nsfw } /// Returns the name of the category. diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index a897002..3f52055 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -14,6 +14,8 @@ use builder::{ }; #[cfg(all(feature = "cache", feature = "model"))] use CACHE; +#[cfg(all(feature = "cache", feature = "model"))] +use Cache; #[cfg(feature = "model")] use http::{self, AttachmentType}; #[cfg(feature = "model")] @@ -293,7 +295,17 @@ impl ChannelId { /// [`Channel`]: ../channel/enum.Channel.html #[cfg(feature = "cache")] #[inline] - pub fn to_channel_cached(self) -> Option<Channel> { CACHE.read().channel(self) } + pub fn to_channel_cached(self) -> Option<Channel> { + self._to_channel_cached(&CACHE) + } + + /// To allow testing pass their own cache instead of using the globale one. + #[cfg(feature = "cache")] + #[inline] + pub(crate) fn _to_channel_cached(self, cache: &RwLock<Cache>) -> Option<Channel> { + cache.read().channel(self) + } + /// Search the cache for the channel. If it can't be found, the channel is /// requested over REST. diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index ad16fa0..e6946df 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -174,12 +174,8 @@ impl Group { /// 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 } diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 3713004..c403b4b 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -386,18 +386,14 @@ impl GuildChannel { /// 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 - #[cfg(feature = "utils")] #[inline] pub fn is_nsfw(&self) -> bool { - self.kind == ChannelType::Text && (self.nsfw || serenity_utils::is_nsfw(&self.name)) + self.kind == ChannelType::Text && self.nsfw } /// Gets a message from the channel. diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 5589ad0..e568a28 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -546,6 +546,15 @@ impl Message { http::unpin_message(self.channel_id.0, self.id.0) } + /// Tries to return author's nickname in the current channel's guild. + /// + /// **Note**: + /// If message was sent in a private channel, then the function will return + /// `None`. + pub fn author_nick(&self) -> Option<String> { + self.guild_id.as_ref().and_then(|guild_id| self.author.nick_in(*guild_id)) + } + pub(crate) fn check_content_length(map: &JsonMap) -> Result<()> { if let Some(content) = map.get("content") { if let Value::String(ref content) = *content { diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 73e2ff3..bc4ea75 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -334,11 +334,7 @@ impl Channel { } /// Determines if the channel is NSFW. - /// - /// Refer to [`utils::is_nsfw`] for more details. - /// - /// [`utils::is_nsfw`]: ../../utils/fn.is_nsfw.html - #[cfg(all(feature = "model", feature = "utils"))] + #[cfg(feature = "model")] #[inline] pub fn is_nsfw(&self) -> bool { match *self { @@ -769,7 +765,7 @@ mod test { #[test] fn nsfw_checks() { let mut channel = guild_channel(); - assert!(channel.is_nsfw()); + assert!(!channel.is_nsfw()); channel.kind = ChannelType::Voice; assert!(!channel.is_nsfw()); @@ -778,7 +774,7 @@ mod test { assert!(!channel.is_nsfw()); channel.name = "nsfw".to_string(); - assert!(channel.is_nsfw()); + assert!(!channel.is_nsfw()); channel.kind = ChannelType::Voice; assert!(!channel.is_nsfw()); channel.kind = ChannelType::Text; diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index 2657d3b..4d602c9 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -150,12 +150,8 @@ impl PrivateChannel { /// 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 } diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 43cde4b..b91e829 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -1957,7 +1957,7 @@ impl Region { Region::EuWest => "eu-west", Region::Frankfurt => "frankfurt", Region::HongKong => "hongkong", - Region::Japan => "Japan", + Region::Japan => "japan", Region::London => "london", Region::Russia => "russia", Region::Singapore => "singapore", diff --git a/src/model/guild/role.rs b/src/model/guild/role.rs index ac969c0..53239a8 100644 --- a/src/model/guild/role.rs +++ b/src/model/guild/role.rs @@ -6,7 +6,7 @@ use builder::EditRole; #[cfg(all(feature = "cache", feature = "model"))] use internal::prelude::*; #[cfg(all(feature = "cache", feature = "model"))] -use {CACHE, http}; +use {CACHE, Cache, http}; #[cfg(all(feature = "cache", feature = "model", feature = "utils"))] use std::str::FromStr; @@ -182,9 +182,12 @@ impl RoleId { /// [`Role`]: ../guild/struct.Role.html #[cfg(feature = "cache")] pub fn to_role_cached(self) -> Option<Role> { - let cache = CACHE.read(); + self._to_role_cached(&CACHE) + } - for guild in cache.guilds.values() { + #[cfg(feature = "cache")] + pub(crate) fn _to_role_cached(self, cache: &RwLock<Cache>) -> Option<Role> { + for guild in cache.read().guilds.values() { let guild = guild.read(); if !guild.roles.contains_key(&self) { diff --git a/src/model/invite.rs b/src/model/invite.rs index a6aa756..b391f9b 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -162,7 +162,7 @@ impl Invite { pub fn url(&self) -> String { format!("https://discord.gg/{}", self.code) } } -/// A inimal information about the channel an invite points to. +/// A minimal information about the channel an invite points to. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InviteChannel { pub id: ChannelId, @@ -253,7 +253,7 @@ pub struct RichInvite { pub max_age: u64, /// The maximum number of times that an invite may be used before it expires. - /// Note that this does not supercede the [`max_age`] value, if the value of + /// Note that this does not supersede the [`max_age`] value, if the value of /// [`temporary`] is `true`. If the value of `temporary` is `false`, then the /// invite _will_ self-expire after the given number of max uses. diff --git a/src/model/misc.rs b/src/model/misc.rs index ff0f3de..9c09a9b 100644 --- a/src/model/misc.rs +++ b/src/model/misc.rs @@ -156,7 +156,7 @@ macro_rules! impl_from_str { type Err = $err; fn from_str(s: &str) -> StdResult<Self, Self::Err> { - Ok(match utils::parse_username(s) { + Ok(match utils::parse_mention(s) { Some(id) => $id(id), None => s.parse::<u64>().map($id).map_err(|_| $err::InvalidFormat)?, }) diff --git a/src/model/permissions.rs b/src/model/permissions.rs index 784071e..b318416 100644 --- a/src/model/permissions.rs +++ b/src/model/permissions.rs @@ -9,7 +9,7 @@ //! presets are available. These are [`PRESET_GENERAL`], [`PRESET_TEXT`], and //! [`PRESET_VOICE`]. //! -//! Permissions follow a heirarchy: +//! Permissions follow a hierarchy: //! //! - An account can grant roles to users that are of a lower position than //! its highest role; diff --git a/src/model/user.rs b/src/model/user.rs index 694f098..2c4ad00 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -612,7 +612,7 @@ impl User { /// /// # Examples /// - /// If maintaing a very long-running bot, you may want to periodically + /// If maintaining a very long-running bot, you may want to periodically /// refresh information about certain users if the state becomes /// out-of-sync: /// @@ -712,6 +712,29 @@ impl User { /// ``` #[inline] pub fn tag(&self) -> String { tag(&self.name, self.discriminator) } + + /// Returns the user's nickname in the given `guild_id`. + /// + /// If none is used, it returns `None`. + #[inline] + pub fn nick_in<G>(&self, guild_id: G) -> Option<String> + where G: Into<GuildId> { + self._nick_in(guild_id.into()) + } + + fn _nick_in(&self, guild_id: GuildId) -> Option<String> { + #[cfg(feature = "cache")] + { + guild_id.to_guild_cached().and_then(|guild| { + guild.read().members.get(&self.id).and_then(|member| member.nick.clone()) + }) + } + + #[cfg(not(feature = "cache"))] + { + guild_id.member(&self.id).and_then(|member| member.nick.clone()) + } + } } impl fmt::Display for User { |