From fcac277fa92d7a046bfcc14524ee236223056d18 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Thu, 29 Dec 2016 17:07:11 -0800 Subject: Add Message::guild() helper --- src/model/channel.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/model/channel.rs b/src/model/channel.rs index acf8d26..910cbbc 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -590,6 +590,20 @@ impl Message { } } + /// Retrieves the guild associated with this message. + /// + /// Returns `None` if the guild data does not exist in the cache, or if the + /// message was not sent in a [`GuildChannel`]. + /// + /// [`GuildChannel`]: struct.GuildChannel.html + #[cfg(all(feature="cache", feature="methods"))] + pub fn guild(&self) -> Option { + match self.guild_id().map(|x| CACHE.read().unwrap().get_guild(x).cloned()) { + Some(Some(guild)) => Some(guild), + _ => None, + } + } + /// Retrieves the Id of the guild that the message was sent in, if sent in /// one. /// @@ -597,9 +611,7 @@ impl Message { /// cache. #[cfg(all(feature="cache", feature="methods"))] pub fn guild_id(&self) -> Option { - let cache = CACHE.read().unwrap(); - - match cache.get_channel(self.channel_id) { + match CACHE.read().unwrap().get_channel(self.channel_id) { Some(ChannelRef::Guild(channel)) => Some(channel.guild_id), _ => None, } -- cgit v1.2.3