aboutsummaryrefslogtreecommitdiff
path: root/src/cache
diff options
context:
space:
mode:
authorAdelyn <[email protected]>2018-08-26 15:25:33 -0600
committerzeyla <[email protected]>2018-08-26 14:25:33 -0700
commit27ccf2c9c8ce785b54595eaabcebf86db07bf5bc (patch)
tree6a19d241626a082ecf06c0cf626463f0ea2df7bd /src/cache
parentMove low-level http functions to a raw module (diff)
downloadserenity-27ccf2c9c8ce785b54595eaabcebf86db07bf5bc.tar.xz
serenity-27ccf2c9c8ce785b54595eaabcebf86db07bf5bc.zip
Old message on update (#368)
Diffstat (limited to 'src/cache')
-rw-r--r--src/cache/mod.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cache/mod.rs b/src/cache/mod.rs
index 780c364..e6e1006 100644
--- a/src/cache/mod.rs
+++ b/src/cache/mod.rs
@@ -576,6 +576,47 @@ impl Cache {
})
}
+ /// Retrieves a [`Channel`]'s message from the cache based on the channel's and
+ /// message's given Ids.
+ ///
+ /// **Note**: This will clone the entire message.
+ ///
+ /// # Examples
+ ///
+ /// Retrieving the message object from a channel, in a
+ /// [`EventHandler::message`] context:
+ ///
+ /// ```rust,no_run
+ /// use serenity::CACHE;
+ ///
+ /// # use serenity::model::id::{ChannelId, MessageId};
+ /// # let message = ChannelId(0).message(MessageId(1)).unwrap();
+ ///
+ /// let cache = CACHE.read();
+ /// let fetched_message = cache.message(message.channel_id, message.id);
+ /// match fetched_message {
+ /// Some(m) => {
+ /// assert_eq!(message.content, m.content);
+ /// },
+ /// None => {
+ /// println!("No message found in cache.");
+ /// }
+ /// }
+ /// ```
+ ///
+ /// [`EventHandler::message`]: ../client/trait.EventHandler.html#method.message
+ /// [`Channel`]: ../model/channel/struct.Channel.html
+ #[inline]
+ pub fn message<C, M>(&self, channel_id: C, message_id: M) -> Option<Message>
+ where C: Into<ChannelId>, M: Into<MessageId> {
+ self._message(channel_id.into(), message_id.into())
+ }
+
+ fn _message(&self, channel_id: ChannelId, message_id: MessageId) -> Option<Message> {
+ self.messages.get(&channel_id).and_then(|messages| {
+ messages.get(&message_id).cloned()
+ })
+ }
/// Retrieves a [`PrivateChannel`] from the cache's [`private_channels`]
/// map, if it exists.
///