diff options
| author | Austin Hellyer <[email protected]> | 2016-12-29 17:07:11 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-12-31 11:12:46 -0800 |
| commit | fcac277fa92d7a046bfcc14524ee236223056d18 (patch) | |
| tree | 536ff58c40c2c6c96f35ec2bdfb18f89436c0bbd /src/model | |
| parent | Fix typo (diff) | |
| download | serenity-fcac277fa92d7a046bfcc14524ee236223056d18.tar.xz serenity-fcac277fa92d7a046bfcc14524ee236223056d18.zip | |
Add Message::guild() helper
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel.rs | 18 |
1 files changed, 15 insertions, 3 deletions
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<Guild> { + 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<GuildId> { - 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, } |