diff options
| author | Zeyla Hellyer <[email protected]> | 2017-06-04 12:02:33 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-06-04 14:38:24 -0700 |
| commit | 063a52f8c028c7432ee556380d2bd5c652d75d22 (patch) | |
| tree | cd3997e32b039d1c63d9947a5f85768388b2fc35 /src | |
| parent | Add some model docs, deprecate Role::edit_role (diff) | |
| download | serenity-063a52f8c028c7432ee556380d2bd5c652d75d22.tar.xz serenity-063a52f8c028c7432ee556380d2bd5c652d75d22.zip | |
Add Message::channel()
The method can be used as a shortcut for retrieving a message's channel
from the cache.
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/channel/message.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index da64086..8b9f0af 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -68,6 +68,49 @@ pub struct Message { #[cfg(feature="model")] impl Message { + /// Retrieves the related channel located in the cache. + /// + /// Returns `None` if the channel is not in the cache. + /// + /// # Examples + /// + /// On command, print the name of the channel that a message took place in: + /// + /// ```rust,no_run + /// # #[macro_use] extern crate serenity; + /// # + /// # use serenity::Client; + /// # + /// # fn main() { + /// # let mut client = Client::login(""); + /// # + /// use serenity::model::Channel; + /// + /// client.with_framework(|f| f + /// .configure(|c| c.prefix("~")) + /// .command("channelname", |c| c.exec(channel_name))); + /// + /// command!(channel_name(_ctx, msg) { + /// let _ = match msg.channel() { + /// Some(Channel::Group(c)) => msg.reply(&c.read().unwrap().name()), + /// Some(Channel::Guild(c)) => msg.reply(&c.read().unwrap().name), + /// Some(Channel::Private(c)) => { + /// let channel = c.read().unwrap(); + /// let user = channel.recipient.read().unwrap(); + /// + /// msg.reply(&format!("DM with {}", user.name.clone())) + /// }, + /// None => msg.reply("Unknown"), + /// }; + /// }); + /// # } + /// ``` + #[cfg(feature="cache")] + #[inline] + pub fn channel(&self) -> Option<Channel> { + CACHE.read().unwrap().channel(self.channel_id) + } + /// Deletes the message. /// /// **Note**: The logged in user must either be the author of the message or |