diff options
| author | Maiddog <[email protected]> | 2017-08-26 17:55:43 -0500 |
|---|---|---|
| committer | alex <[email protected]> | 2017-08-27 00:55:43 +0200 |
| commit | 3e0b1032d80a1847558a752e8316d97f9ae58f04 (patch) | |
| tree | ca65390091cb3c0ab98b6497a1447ba69df3d20d /src/framework/standard | |
| parent | Use `$crate` for `Args` (diff) | |
| download | serenity-3e0b1032d80a1847558a752e8316d97f9ae58f04.tar.xz serenity-3e0b1032d80a1847558a752e8316d97f9ae58f04.zip | |
Add ability to play DCA and Opus files. (#148)
Diffstat (limited to 'src/framework/standard')
| -rw-r--r-- | src/framework/standard/buckets.rs | 6 | ||||
| -rw-r--r-- | src/framework/standard/command.rs | 5 | ||||
| -rw-r--r-- | src/framework/standard/configuration.rs | 48 | ||||
| -rw-r--r-- | src/framework/standard/create_command.rs | 11 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 11 | ||||
| -rw-r--r-- | src/framework/standard/help_commands.rs | 18 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 75 |
7 files changed, 108 insertions, 66 deletions
diff --git a/src/framework/standard/buckets.rs b/src/framework/standard/buckets.rs index 4719b85..f2c4486 100644 --- a/src/framework/standard/buckets.rs +++ b/src/framework/standard/buckets.rs @@ -31,9 +31,9 @@ pub(crate) struct Bucket { impl Bucket { pub fn take(&mut self, user_id: u64) -> i64 { let time = Utc::now().timestamp(); - let user = self.users - .entry(user_id) - .or_insert_with(MemberRatelimit::default); + let user = self.users.entry(user_id).or_insert_with( + MemberRatelimit::default, + ); if let Some((timespan, limit)) = self.ratelimit.limit { if (user.tickets + 1) > limit { diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 8c23ca0..22a82ce 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -6,7 +6,10 @@ use std::collections::HashMap; pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool + 'static; pub type Exec = Fn(&mut Context, &Message, Args) -> Result<(), String> + 'static; -pub type Help = Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args) +pub type Help = Fn(&mut Context, + &Message, + HashMap<String, Arc<CommandGroup>>, + Args) -> Result<(), String> + 'static; pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + 'static; diff --git a/src/framework/standard/configuration.rs b/src/framework/standard/configuration.rs index dd1c7e7..491bbc4 100644 --- a/src/framework/standard/configuration.rs +++ b/src/framework/standard/configuration.rs @@ -33,19 +33,32 @@ use model::{GuildId, Message, UserId}; /// [`Client`]: ../../client/struct.Client.html /// [`Framework`]: struct.Framework.html pub struct Configuration { - #[doc(hidden)] pub allow_dm: bool, - #[doc(hidden)] pub allow_whitespace: bool, - #[doc(hidden)] pub blocked_guilds: HashSet<GuildId>, - #[doc(hidden)] pub blocked_users: HashSet<UserId>, - #[doc(hidden)] pub depth: usize, - #[doc(hidden)] pub disabled_commands: HashSet<String>, - #[doc(hidden)] pub dynamic_prefix: Option<Box<PrefixCheck>>, - #[doc(hidden)] pub ignore_bots: bool, - #[doc(hidden)] pub ignore_webhooks: bool, - #[doc(hidden)] pub on_mention: Option<Vec<String>>, - #[doc(hidden)] pub owners: HashSet<UserId>, - #[doc(hidden)] pub prefixes: Vec<String>, - #[doc(hidden)] pub delimiters: Vec<String>, + #[doc(hidden)] + pub allow_dm: bool, + #[doc(hidden)] + pub allow_whitespace: bool, + #[doc(hidden)] + pub blocked_guilds: HashSet<GuildId>, + #[doc(hidden)] + pub blocked_users: HashSet<UserId>, + #[doc(hidden)] + pub depth: usize, + #[doc(hidden)] + pub disabled_commands: HashSet<String>, + #[doc(hidden)] + pub dynamic_prefix: Option<Box<PrefixCheck>>, + #[doc(hidden)] + pub ignore_bots: bool, + #[doc(hidden)] + pub ignore_webhooks: bool, + #[doc(hidden)] + pub on_mention: Option<Vec<String>>, + #[doc(hidden)] + pub owners: HashSet<UserId>, + #[doc(hidden)] + pub prefixes: Vec<String>, + #[doc(hidden)] + pub delimiters: Vec<String>, } impl Configuration { @@ -256,8 +269,8 @@ impl Configuration { if let Ok(current_user) = http::get_current_user() { self.on_mention = Some(vec![ - format!("<@{}>", current_user.id), // Regular mention - format!("<@!{}>", current_user.id), // Nickname mention + format!("<@{}>", current_user.id), // Regular mention + format!("<@!{}>", current_user.id) /* Nickname mention */, ]); } @@ -402,8 +415,9 @@ impl Configuration { /// ``` pub fn delimiters(mut self, delimiters: Vec<&str>) -> Self { self.delimiters.clear(); - self.delimiters - .extend(delimiters.into_iter().map(|s| s.to_string())); + self.delimiters.extend( + delimiters.into_iter().map(|s| s.to_string()), + ); self } diff --git a/src/framework/standard/create_command.rs b/src/framework/standard/create_command.rs index 97bf092..0a467e1 100644 --- a/src/framework/standard/create_command.rs +++ b/src/framework/standard/create_command.rs @@ -11,9 +11,9 @@ pub struct CreateCommand(pub Command); impl CreateCommand { /// Adds multiple aliases. pub fn batch_known_as(mut self, names: Vec<&str>) -> Self { - self.0 - .aliases - .extend(names.into_iter().map(|n| n.to_owned())); + self.0.aliases.extend( + names.into_iter().map(|n| n.to_owned()), + ); self } @@ -115,7 +115,10 @@ 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>>, Args) + where F: Fn(&mut Context, + &Message, + HashMap<String, Arc<CommandGroup>>, + Args) -> Result<(), String> + 'static { self.0.exec = CommandType::WithCommands(Box::new(f)); diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index d533c6a..013ba67 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -35,7 +35,9 @@ impl CreateGroup { 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())), + CommandOrAlias::Alias( + format!("{} {}", prefix, command_name.to_string()), + ), ); } else { self.0.commands.insert( @@ -59,9 +61,10 @@ impl CreateGroup { where F: Fn(&mut Context, &Message, Args) -> Result<(), String> + Send + Sync + 'static { let cmd = Arc::new(Command::new(f)); - self.0 - .commands - .insert(command_name.to_owned(), CommandOrAlias::Command(cmd)); + self.0.commands.insert( + command_name.to_owned(), + CommandOrAlias::Command(cmd), + ); self } diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index 75c5b0b..f081e1a 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -116,15 +116,19 @@ pub fn with_embeds(_: &mut Context, if let Some(ref usage) = command.usage { embed = embed.field(|f| { - f.name("Usage") - .value(&format!("`{} {}`", command_name, usage)) + f.name("Usage").value( + &format!("`{} {}`", command_name, usage), + ) }); } if let Some(ref example) = command.example { embed = embed.field(|f| { - f.name("Sample usage") - .value(&format!("`{} {}`", command_name, example)) + f.name("Sample usage").value(&format!( + "`{} {}`", + command_name, + example + )) }); } @@ -293,8 +297,10 @@ pub fn plain(_: &mut Context, } } - let _ = msg.channel_id - .say(&format!("**Error**: Command `{}` not found.", name)); + let _ = msg.channel_id.say(&format!( + "**Error**: Command `{}` not found.", + name + )); return Ok(()); } diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index c67e128..04ebc5f 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -402,9 +402,9 @@ impl StandardFramework { } if let Some(guild) = guild_id.find() { - return self.configuration - .blocked_users - .contains(&guild.with(|g| g.owner_id)); + return self.configuration.blocked_users.contains( + &guild.with(|g| g.owner_id), + ); } } @@ -415,8 +415,9 @@ impl StandardFramework { fn has_correct_permissions(&self, command: &Arc<Command>, message: &Message) -> bool { if !command.required_permissions.is_empty() { if let Some(guild) = message.guild() { - let perms = guild - .with(|g| g.permissions_for(message.channel_id, message.author.id)); + let perms = guild.with(|g| { + g.permissions_for(message.channel_id, message.author.id) + }); return perms.contains(command.required_permissions); } @@ -446,7 +447,8 @@ impl StandardFramework { let rate_limit = bucket.take(message.author.id.0); match bucket.check { Some(ref check) => { - let apply = feature_cache! {{ + let apply = + feature_cache! {{ let guild_id = message.guild_id(); (check)(context, guild_id, message.channel_id, message.author.id) } else { @@ -457,8 +459,10 @@ impl StandardFramework { 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)); + } }, } } @@ -508,19 +512,18 @@ impl StandardFramework { if command.owners_only { Some(DispatchError::OnlyForOwners) - } else if self.configuration - .blocked_users - .contains(&message.author.id) { + } else if self.configuration.blocked_users.contains( + &message.author.id, + ) { Some(DispatchError::BlockedUser) } else if self.configuration.disabled_commands.contains(to_check) { Some(DispatchError::CommandDisabled(to_check.to_owned())) } else if self.configuration.disabled_commands.contains(built) { Some(DispatchError::CommandDisabled(built.to_owned())) } else { - let all_passed = command - .checks - .iter() - .all(|check| check(&mut context, message, args, command)); + let all_passed = command.checks.iter().all(|check| { + check(&mut context, message, args, command) + }); if all_passed { None @@ -574,16 +577,21 @@ impl StandardFramework { pub fn on<F, S>(mut self, command_name: S, f: F) -> Self where F: Fn(&mut Context, &Message, Args) -> Result<(), String> + 'static, S: Into<String> { { - let ungrouped = self.groups - .entry("Ungrouped".to_owned()) - .or_insert_with(|| Arc::new(CommandGroup::default())); + let ungrouped = self.groups.entry("Ungrouped".to_owned()).or_insert_with( + || { + Arc::new(CommandGroup::default()) + }, + ); if let Some(ref mut group) = Arc::get_mut(ungrouped) { let name = command_name.into(); - group - .commands - .insert(name, CommandOrAlias::Command(Arc::new(Command::new(f)))); + group.commands.insert( + name, + CommandOrAlias::Command( + Arc::new(Command::new(f)), + ), + ); } } @@ -606,9 +614,11 @@ impl StandardFramework { pub fn command<F, S>(mut self, command_name: S, f: F) -> Self where F: FnOnce(CreateCommand) -> CreateCommand, S: Into<String> { { - let ungrouped = self.groups - .entry("Ungrouped".to_owned()) - .or_insert_with(|| Arc::new(CommandGroup::default())); + let ungrouped = self.groups.entry("Ungrouped".to_owned()).or_insert_with( + || { + Arc::new(CommandGroup::default()) + }, + ); if let Some(ref mut group) = Arc::get_mut(ungrouped) { let cmd = f(CreateCommand(Command::default())).0; @@ -623,15 +633,17 @@ impl StandardFramework { } } else { for v in &cmd.aliases { - group - .commands - .insert(v.to_owned(), CommandOrAlias::Alias(name.clone())); + group.commands.insert( + v.to_owned(), + CommandOrAlias::Alias(name.clone()), + ); } } - group - .commands - .insert(name, CommandOrAlias::Command(Arc::new(cmd))); + group.commands.insert( + name, + CommandOrAlias::Command(Arc::new(cmd)), + ); } } @@ -844,7 +856,8 @@ impl Framework for StandardFramework { for group in groups.values() { let command_length = built.len(); - if let Some(&CommandOrAlias::Alias(ref points_to)) = group.commands.get(&built) { + if let Some(&CommandOrAlias::Alias(ref points_to)) = + group.commands.get(&built) { built = points_to.to_owned(); } |