aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLakelezz <[email protected]>2018-11-11 17:38:12 +0100
committerKen Swenson <[email protected]>2018-11-11 11:39:21 -0500
commit05c84e63572fb09e6aa5760696fefbbb8d20a69c (patch)
tree2d1307b81f65d83e1861e832d282243b3adb19d3 /src
parentUpdate `base64` and `sodiumoxide` (#431) (diff)
downloadserenity-05c84e63572fb09e6aa5760696fefbbb8d20a69c.tar.xz
serenity-05c84e63572fb09e6aa5760696fefbbb8d20a69c.zip
Add Nick Methods for `Message` and `UserId` (#432)
Diffstat (limited to 'src')
-rw-r--r--src/model/channel/message.rs9
-rw-r--r--src/model/user.rs23
2 files changed, 32 insertions, 0 deletions
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs
index c605b18..c8c0998 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/user.rs b/src/model/user.rs
index af09d4e..5020cdf 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -718,6 +718,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 {