diff options
| author | acdenisSK <[email protected]> | 2017-07-27 08:10:41 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-07-27 08:10:41 +0200 |
| commit | 70b5097aaac85f970c32ceb988dbb5f5d575ee0f (patch) | |
| tree | d2f391d3b552cfae58b74748a2a2aa5ae80c7986 /src/framework | |
| parent | rustfmt (diff) | |
| download | serenity-70b5097aaac85f970c32ceb988dbb5f5d575ee0f.tar.xz serenity-70b5097aaac85f970c32ceb988dbb5f5d575ee0f.zip | |
Change the config a bit, and a few nitpicks
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/buckets.rs | 12 | ||||
| -rw-r--r-- | src/framework/command.rs | 3 | ||||
| -rw-r--r-- | src/framework/configuration.rs | 3 | ||||
| -rw-r--r-- | src/framework/create_command.rs | 18 | ||||
| -rw-r--r-- | src/framework/create_group.rs | 31 | ||||
| -rw-r--r-- | src/framework/help_commands.rs | 28 | ||||
| -rw-r--r-- | src/framework/mod.rs | 176 |
7 files changed, 135 insertions, 136 deletions
diff --git a/src/framework/buckets.rs b/src/framework/buckets.rs index 95b832b..4719b85 100644 --- a/src/framework/buckets.rs +++ b/src/framework/buckets.rs @@ -4,6 +4,12 @@ use std::default::Default; use client::Context; use model::{ChannelId, GuildId, UserId}; +#[cfg(feature = "cache")] +type Check = Fn(&mut Context, Option<GuildId>, ChannelId, UserId) -> bool + 'static; + +#[cfg(not(feature = "cache"))] +type Check = Fn(&mut Context, ChannelId, UserId) -> bool + 'static; + pub(crate) struct Ratelimit { pub delay: i64, pub limit: Option<(i64, i32)>, @@ -19,11 +25,7 @@ pub(crate) struct MemberRatelimit { pub(crate) struct Bucket { pub ratelimit: Ratelimit, pub users: HashMap<u64, MemberRatelimit>, - #[cfg(feature = "cache")] - pub check: - Option<Box<Fn(&mut Context, Option<GuildId>, ChannelId, UserId) -> bool + 'static>>, - #[cfg(not(feature = "cache"))] - pub checK: Option<Box<Fn(&mut Context, ChannelId, UserId) -> bool + 'static>>, + pub check: Option<Box<Check>>, } impl Bucket { diff --git a/src/framework/command.rs b/src/framework/command.rs index 1484891..4e338d4 100644 --- a/src/framework/command.rs +++ b/src/framework/command.rs @@ -69,8 +69,7 @@ pub struct Command { impl Command { pub fn new<F>(f: F) -> Self - where - F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + 'static, { + where F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + 'static { Command { aliases: Vec::new(), checks: Vec::default(), diff --git a/src/framework/configuration.rs b/src/framework/configuration.rs index 06e7e6a..28e1af5 100644 --- a/src/framework/configuration.rs +++ b/src/framework/configuration.rs @@ -215,8 +215,7 @@ impl Configuration { /// }))); /// ``` pub fn dynamic_prefix<F>(mut self, dynamic_prefix: F) -> Self - where - F: Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static, { + where F: Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static { self.dynamic_prefix = Some(Box::new(dynamic_prefix)); self diff --git a/src/framework/create_command.rs b/src/framework/create_command.rs index d97f6fa..d2ae31f 100644 --- a/src/framework/create_command.rs +++ b/src/framework/create_command.rs @@ -65,8 +65,7 @@ impl CreateCommand { /// } /// ``` pub fn check<F>(mut self, check: F) -> Self - where - F: Fn(&mut Context, &Message, &Arc<Command>) -> bool + Send + Sync + 'static, { + where F: Fn(&mut Context, &Message, &Arc<Command>) -> bool + Send + Sync + 'static { self.0.checks.push(Box::new(check)); self @@ -100,8 +99,10 @@ impl CreateCommand { /// /// [`exec_str`]: #method.exec_str pub fn exec<F>(mut self, func: F) -> Self - where - F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static, { + where F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + + Send + + Sync + + 'static { self.0.exec = CommandType::Basic(Box::new(func)); self @@ -113,12 +114,9 @@ impl CreateCommand { /// /// You can return `Err(string)` if there's an error. pub fn exec_help<F>(mut self, f: F) -> Self - where - F: Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, &[String]) - -> Result<(), String> - + Send - + Sync - + 'static, { + where F: Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, &[String]) + -> Result<(), String> + + 'static { self.0.exec = CommandType::WithCommands(Box::new(f)); self diff --git a/src/framework/create_group.rs b/src/framework/create_group.rs index d5b68c3..5c36e4d 100644 --- a/src/framework/create_group.rs +++ b/src/framework/create_group.rs @@ -27,26 +27,27 @@ pub struct CreateGroup(pub CommandGroup); impl CreateGroup { /// Adds a command to group. pub fn command<F>(mut self, command_name: &str, f: F) -> Self - where - F: FnOnce(CreateCommand) -> CreateCommand, { + where F: FnOnce(CreateCommand) -> CreateCommand { let cmd = f(CreateCommand(Command::default())).0; for n in &cmd.aliases { if let Some(ref prefix) = self.0.prefix { - self.0 - .commands - .insert(format!("{} {}", prefix, n.to_owned()), - CommandOrAlias::Alias(format!("{} {}", - prefix, - command_name.to_string()))); + self.0.commands.insert( + format!("{} {}", prefix, n.to_owned()), + CommandOrAlias::Alias(format!("{} {}", prefix, command_name.to_string())), + ); } else { - self.0.commands.insert(n.to_owned(), - CommandOrAlias::Alias(command_name.to_string())); + self.0.commands.insert( + n.to_owned(), + CommandOrAlias::Alias(command_name.to_string()), + ); } } - self.0.commands.insert(command_name.to_owned(), - CommandOrAlias::Command(Arc::new(cmd))); + self.0.commands.insert( + command_name.to_owned(), + CommandOrAlias::Command(Arc::new(cmd)), + ); self } @@ -54,8 +55,10 @@ impl CreateGroup { /// Adds a command to group with simplified API. /// You can return Err(string) if there's an error. pub fn on<F>(mut self, command_name: &str, f: F) -> Self - where - F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static, { + where F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + + Send + + Sync + + 'static { let cmd = Arc::new(Command::new(f)); self.0 diff --git a/src/framework/help_commands.rs b/src/framework/help_commands.rs index 7f863b4..6db3561 100644 --- a/src/framework/help_commands.rs +++ b/src/framework/help_commands.rs @@ -33,8 +33,9 @@ use model::{ChannelId, Message}; use utils::Colour; fn error_embed(channel_id: &ChannelId, input: &str) { - let _ = - channel_id.send_message(|m| m.embed(|e| e.colour(Colour::dark_red()).description(input))); + let _ = channel_id.send_message(|m| { + m.embed(|e| e.colour(Colour::dark_red()).description(input)) + }); } fn remove_aliases(cmds: &HashMap<String, CommandOrAlias>) -> HashMap<&String, &InternalCommand> { @@ -158,9 +159,10 @@ pub fn with_embeds(_: &mut Context, let _ = msg.channel_id.send_message(|m| { m.embed(|mut e| { - e = e.colour(Colour::rosewater()) - .description("To get help with an individual command, pass its \ - name as an argument to this command."); + e = e.colour(Colour::rosewater()).description( + "To get help with an individual command, pass its \ + name as an argument to this command.", + ); let mut group_names = groups.keys().collect::<Vec<_>>(); group_names.sort(); @@ -275,14 +277,16 @@ pub fn plain(_: &mut Context, let _ = write!(result, "**Group:** {}\n", group_name); } + let only = if command.dm_only { + "Only in DM" + } else if command.guild_only { + "Only in guilds" + } else { + "In DM and guilds" + }; + result.push_str("**Available:** "); - result.push_str(if command.dm_only { - "Only in DM" - } else if command.guild_only { - "Only in guilds" - } else { - "In DM and guilds" - }); + result.push_str(only); result.push_str("\n"); let _ = msg.channel_id.say(&result); diff --git a/src/framework/mod.rs b/src/framework/mod.rs index 9fddc76..14533b5 100644 --- a/src/framework/mod.rs +++ b/src/framework/mod.rs @@ -277,8 +277,7 @@ impl BuiltinFramework { /// [`prefix`]: struct.Configuration.html#method.prefix /// [allowing whitespace]: struct.Configuration.html#method.allow_whitespace pub fn configure<F>(mut self, f: F) -> Self - where - F: FnOnce(Configuration) -> Configuration, { + where F: FnOnce(Configuration) -> Configuration { self.configuration = f(self.configuration); self @@ -308,17 +307,18 @@ impl BuiltinFramework { /// .exec_str("pong!"))); /// ``` pub fn bucket<S>(mut self, s: S, delay: i64, time_span: i64, limit: i32) -> Self - where - S: Into<String>, { - self.buckets.insert(s.into(), - Bucket { - ratelimit: Ratelimit { - delay: delay, - limit: Some((time_span, limit)), - }, - users: HashMap::new(), - check: None, - }); + where S: Into<String> { + self.buckets.insert( + s.into(), + Bucket { + ratelimit: Ratelimit { + delay: delay, + limit: Some((time_span, limit)), + }, + users: HashMap::new(), + check: None, + }, + ); self } @@ -359,18 +359,19 @@ impl BuiltinFramework { limit: i32, check: Check) -> Self - where - Check: Fn(&mut Context, Option<GuildId>, ChannelId, UserId) -> bool + 'static, - S: Into<String>, { - self.buckets.insert(s.into(), - Bucket { - ratelimit: Ratelimit { - delay, - limit: Some((time_span, limit)), - }, - users: HashMap::new(), - check: Some(Box::new(check)), - }); + where Check: Fn(&mut Context, Option<GuildId>, ChannelId, UserId) -> bool + 'static, + S: Into<String> { + self.buckets.insert( + s.into(), + Bucket { + ratelimit: Ratelimit { + delay, + limit: Some((time_span, limit)), + }, + users: HashMap::new(), + check: Some(Box::new(check)), + }, + ); self } @@ -409,18 +410,18 @@ impl BuiltinFramework { limit: i32, check: Check) -> Self - where - Check: Fn(&mut Context, ChannelId, UserId) -> bool + 'static, - S: Into<String>, { - self.buckets.insert(s.into(), - Bucket { - ratelimit: Ratelimit { - delay, - limit: Some((time_span, limit)), - }, - users: HashMap::new(), - check: Some(Box::new(check)), - }); + where Check: Fn(&mut Context, ChannelId, UserId) -> bool + 'static, S: Into<String> { + self.buckets.insert( + s.into(), + Bucket { + ratelimit: Ratelimit { + delay, + limit: Some((time_span, limit)), + }, + users: HashMap::new(), + check: Some(Box::new(check)), + }, + ); self } @@ -447,17 +448,18 @@ impl BuiltinFramework { /// .exec_str("pong!"))); /// ``` pub fn simple_bucket<S>(mut self, s: S, delay: i64) -> Self - where - S: Into<String>, { - self.buckets.insert(s.into(), - Bucket { - ratelimit: Ratelimit { - delay: delay, - limit: None, - }, - users: HashMap::new(), - check: None, - }); + where S: Into<String> { + self.buckets.insert( + s.into(), + Bucket { + ratelimit: Ratelimit { + delay: delay, + limit: None, + }, + users: HashMap::new(), + check: None, + }, + ); self } @@ -516,23 +518,19 @@ impl BuiltinFramework { if let Some(ref mut bucket) = self.buckets.get_mut(bucket) { let rate_limit = bucket.take(message.author.id.0); match bucket.check { - Some(ref check) => { - if feature_cache! {{ + Some(ref check) => if feature_cache! {{ let guild_id = message.guild_id(); (check)(context, guild_id, message.channel_id, message.author.id) } else { (check)(context, message.channel_id, message.author.id) }} { - if rate_limit > 0i64 { - return Some(DispatchError::RateLimited(rate_limit)); - } - } - }, - None => { if rate_limit > 0i64 { return Some(DispatchError::RateLimited(rate_limit)); } }, + None => if rate_limit > 0i64 { + return Some(DispatchError::RateLimited(rate_limit)); + }, } } } @@ -540,18 +538,18 @@ impl BuiltinFramework { if let Some(x) = command.min_args { if args < x as usize { return Some(DispatchError::NotEnoughArguments { - min: x, - given: args, - }); + min: x, + given: args, + }); } } if let Some(x) = command.max_args { if args > x as usize { return Some(DispatchError::TooManyArguments { - max: x, - given: args, - }); + max: x, + given: args, + }); } } @@ -562,7 +560,9 @@ impl BuiltinFramework { } if !self.has_correct_permissions(command, message) { - return Some(DispatchError::LackOfPermissions(command.required_permissions)); + return Some(DispatchError::LackOfPermissions( + command.required_permissions, + )); } if (!self.configuration.allow_dm && message.is_private()) || @@ -636,9 +636,8 @@ impl BuiltinFramework { /// # } /// ``` pub fn on<F, S>(mut self, command_name: S, f: F) -> Self - where - F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + 'static, - S: Into<String>, { + where F: Fn(&mut Context, &Message, Vec<String>) -> Result<(), String> + 'static, + S: Into<String> { { let ungrouped = self.groups .entry("Ungrouped".to_owned()) @@ -670,9 +669,7 @@ impl BuiltinFramework { /// })); /// ``` pub fn command<F, S>(mut self, command_name: S, f: F) -> Self - where - F: FnOnce(CreateCommand) -> CreateCommand, - S: Into<String>, { + where F: FnOnce(CreateCommand) -> CreateCommand, S: Into<String> { { let ungrouped = self.groups .entry("Ungrouped".to_owned()) @@ -684,10 +681,10 @@ impl BuiltinFramework { if let Some(ref prefix) = group.prefix { for v in &cmd.aliases { - group - .commands - .insert(format!("{} {}", prefix, v.to_owned()), - CommandOrAlias::Alias(format!("{} {}", prefix, name))); + group.commands.insert( + format!("{} {}", prefix, v.to_owned()), + CommandOrAlias::Alias(format!("{} {}", prefix, name)), + ); } } else { for v in &cmd.aliases { @@ -730,9 +727,7 @@ impl BuiltinFramework { /// .command("pong", |c| c.exec_str("ping!")))); /// ``` pub fn group<F, S>(mut self, group_name: S, f: F) -> Self - where - F: FnOnce(CreateGroup) -> CreateGroup, - S: Into<String>, { + where F: FnOnce(CreateGroup) -> CreateGroup, S: Into<String> { let group = f(CreateGroup(CommandGroup::default())).0; self.groups.insert(group_name.into(), Arc::new(group)); @@ -777,8 +772,7 @@ impl BuiltinFramework { /// })); /// ``` pub fn on_dispatch_error<F>(mut self, f: F) -> Self - where - F: Fn(Context, Message, DispatchError) + 'static, { + where F: Fn(Context, Message, DispatchError) + 'static { self.dispatch_error_handler = Some(Arc::new(f)); self @@ -834,8 +828,7 @@ impl BuiltinFramework { /// ``` /// pub fn before<F>(mut self, f: F) -> Self - where - F: Fn(&mut Context, &Message, &String) -> bool + 'static, { + where F: Fn(&mut Context, &Message, &String) -> bool + 'static { self.before = Some(Arc::new(f)); self @@ -866,8 +859,7 @@ impl BuiltinFramework { /// })); /// ``` pub fn after<F>(mut self, f: F) -> Self - where - F: Fn(&mut Context, &Message, &String, Result<(), String>) + 'static, { + where F: Fn(&mut Context, &Message, &String, Result<(), String>) + 'static { self.after = Some(Arc::new(f)); self @@ -906,9 +898,9 @@ impl ::Framework for BuiltinFramework { } built.push_str(match round.get(i) { - Some(piece) => piece, - None => continue 'outer, - }); + Some(piece) => piece, + None => continue 'outer, + }); let groups = self.groups.clone(); @@ -950,12 +942,14 @@ impl ::Framework for BuiltinFramework { } }; - if let Some(error) = self.should_fail(&mut context, - &message, - &command, - args.len(), - &to_check, - &built) { + if let Some(error) = self.should_fail( + &mut context, + &message, + &command, + args.len(), + &to_check, + &built, + ) { if let Some(ref handler) = self.dispatch_error_handler { handler(context, message, error); } |