aboutsummaryrefslogtreecommitdiff
path: root/src/ext/cache
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-26 10:32:01 -0800
committerAustin Hellyer <[email protected]>2016-11-26 10:32:01 -0800
commit343f20d2aa18696b961b6e4b3b867b68da2ca717 (patch)
tree0d6a4392c84be60bbba387d162f5ea62aa2f6e17 /src/ext/cache
parentMore widely support no-cache method compiles (diff)
downloadserenity-343f20d2aa18696b961b6e4b3b867b68da2ca717.tar.xz
serenity-343f20d2aa18696b961b6e4b3b867b68da2ca717.zip
Add Cache::get_guild_channel
This is an 'extras'-enabled function to get a guild's channel. This will allow a simple Option pattern match over the result, rather than working with an Option<Channel<T>>. Example of old code vs. new code, in the context of an event handler: Old: ```rs use serenity::cache::CACHE; use serenity::model::Channel; let cache = CACHE.read().unwrap(); let channel = match cache.get_channel(channel_id) { Some(Channel::Guild(channel)) => channel, Some(Channel::Private(_)) => { context.say("Groups and DMs not supported") .map_err(|x| println!("Err sending message: {:?}", why); return; }, None => { context.say("Could not find channel") .map_err(|x| println!("Err sending message: {:?}", x); return; }, } ``` New: ```rs use serenity::cache::CACHE; let cache = CACHE.read().unwrap(); let channel = match cache.get_guild_channel(channel_id) { Some(channel) => channel, None => { context.say("Could not find channel") .map_err(|x| println!("Err sending message: {:?}", x)); return; }, }; ```
Diffstat (limited to 'src/ext/cache')
-rw-r--r--src/ext/cache/mod.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/ext/cache/mod.rs b/src/ext/cache/mod.rs
index 7c4d3a5..ac0946b 100644
--- a/src/ext/cache/mod.rs
+++ b/src/ext/cache/mod.rs
@@ -132,6 +132,19 @@ impl Cache {
self.guilds.get(&id.into())
}
+ #[cfg(feature = "extras")]
+ pub fn get_guild_channel<C: Into<ChannelId>>(&self, id: C) -> Option<&GuildChannel> {
+ let id = id.into();
+
+ for guild in self.guilds.values() {
+ if let Some(channel) = guild.channels.get(&id) {
+ return Some(channel);
+ }
+ }
+
+ None
+ }
+
pub fn get_group<C: Into<ChannelId>>(&self, id: C) -> Option<&Group> {
self.groups.get(&id.into())
}