From bceb049bb2b804dac975567bb7eac6afcfc28574 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Thu, 1 Dec 2016 20:12:27 -0800 Subject: Add Message::guild_id() Searches the Cache for the Id of the guild that the message was posted to - if any - and if the guild and channel data are cached. Requires the `cache` and `method` feature flags be enabled. --- src/model/channel.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src') diff --git a/src/model/channel.rs b/src/model/channel.rs index 13cf506..99fa8c5 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -33,6 +33,8 @@ use ::utils::builder::{CreateEmbed, CreateInvite, EditChannel}; use ::client::CACHE; #[cfg(all(feature = "methods"))] use ::client::rest; +#[cfg(all(feature = "cache", feature = "methods"))] +use ::ext::cache::ChannelRef; impl Attachment { /// If this attachment is an image, then a tuple of the width and height @@ -528,6 +530,21 @@ impl Message { } } + /// Retrieves the Id of the guild that the message was sent in, if sent in + /// one. + /// + /// Returns `None` if the channel data or guild data does not exist in the + /// 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) { + Some(ChannelRef::Guild(channel)) => Some(channel.guild_id), + _ => None, + } + } + /// Checks the length of a string to ensure that it is within Discord's /// maximum message length limit. /// -- cgit v1.2.3