aboutsummaryrefslogtreecommitdiff
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
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; }, }; ```
-rw-r--r--examples/07_voice.rs60
-rw-r--r--src/ext/cache/mod.rs13
2 files changed, 33 insertions, 40 deletions
diff --git a/examples/07_voice.rs b/examples/07_voice.rs
index c352daa..5938c2f 100644
--- a/examples/07_voice.rs
+++ b/examples/07_voice.rs
@@ -5,16 +5,16 @@ extern crate serenity;
#[cfg(feature = "voice")]
use serenity::client::{CACHE, Client, Context};
#[cfg(feature = "voice")]
-use serenity::model::{Channel, ChannelId, Message};
+use serenity::model::{ChannelId, Message};
#[cfg(feature = "voice")]
use std::env;
-#[cfg(not(feature = "voice"))]
+#[cfg(any(not(feature = "extras"), not(feature = "voice")))]
fn main() {
- panic!("Voice not enabled");
+ panic!("'extras' and 'voice' must be enabled");
}
-#[cfg(feature = "voice")]
+#[cfg(all(feature = "extras", feature = "voice"))]
fn main() {
// Configure the client with your Discord bot token in the environment.
let token = env::var("DISCORD_TOKEN")
@@ -36,17 +36,12 @@ fn main() {
let _ = client.start();
}
-#[cfg(feature = "voice")]
+#[cfg(all(feature = "extras", feature = "voice"))]
fn deafen(context: Context, message: Message, _args: Vec<String>) {
- let guild_id = match CACHE.read().unwrap().get_channel(message.channel_id) {
- Some(Channel::Guild(channel)) => channel.guild_id,
- Some(_) => {
- let _ = message.reply("Groups and DMs not supported");
-
- return;
- },
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) {
+ Some(channel) => channel.guild_id,
None => {
- let _ = context.say("Can't find guild");
+ let _ = context.say("Groups and DMs not supported");
return;
},
@@ -72,7 +67,7 @@ fn deafen(context: Context, message: Message, _args: Vec<String>) {
}
}
-#[cfg(feature = "voice")]
+#[cfg(all(feature = "extras", feature = "voice"))]
fn join(context: Context, message: Message, args: Vec<String>) {
let connect_to = match args.get(0) {
Some(arg) => match arg.parse::<u64>() {
@@ -90,15 +85,10 @@ fn join(context: Context, message: Message, args: Vec<String>) {
},
};
- let guild_id = match CACHE.read().unwrap().get_channel(message.channel_id) {
- Some(Channel::Guild(channel)) => channel.guild_id,
- Some(_) => {
- let _ = context.say("Groups and DMs not supported");
-
- return;
- },
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) {
+ Some(channel) => channel.guild_id,
None => {
- let _ = context.say("Can't find guild");
+ let _ = context.say("Groups and DMs not supported");
return;
},
@@ -112,17 +102,12 @@ fn join(context: Context, message: Message, args: Vec<String>) {
let _ = context.say(&format!("Joined {}", connect_to.mention()));
}
-#[cfg(feature = "voice")]
+#[cfg(all(feature = "extras", feature = "voice"))]
fn leave(context: Context, message: Message, _args: Vec<String>) {
- let guild_id = match CACHE.read().unwrap().get_channel(message.channel_id) {
- Some(Channel::Guild(channel)) => channel.guild_id,
- Some(_) => {
- let _ = context.say("Groups and DMs not supported");
-
- return;
- },
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) {
+ Some(channel) => channel.guild_id,
None => {
- let _ = context.say("Can't find guild");
+ let _ = context.say("Groups and DMs not supported");
return;
},
@@ -142,17 +127,12 @@ fn leave(context: Context, message: Message, _args: Vec<String>) {
}
}
-#[cfg(feature = "voice")]
+#[cfg(all(feature = "extras", feature = "voice"))]
fn mute(context: Context, message: Message, _args: Vec<String>) {
- let guild_id = match CACHE.read().unwrap().get_channel(message.channel_id) {
- Some(Channel::Guild(channel)) => channel.guild_id,
- Some(_) => {
- let _ = message.reply("Groups and DMs not supported");
-
- return;
- },
+ let guild_id = match CACHE.read().unwrap().get_guild_channel(message.channel_id) {
+ Some(channel) => channel.guild_id,
None => {
- let _ = context.say("Can't find guild");
+ let _ = context.say("Groups and DMs not supported");
return;
},
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())
}