diff options
| author | Mishio595 <[email protected]> | 2018-07-12 11:35:39 -0600 |
|---|---|---|
| committer | Mishio595 <[email protected]> | 2018-07-12 11:35:39 -0600 |
| commit | 026a37ffe12096a1da263b028df48a8d2909baa3 (patch) | |
| tree | 1d1bf8f9c505408f13b7fd6f877bcf3fd15fa43c /src/framework/standard | |
| parent | Add 3rd param to MessageUpdateEvent Option that contains the old message if f... (diff) | |
| download | serenity-026a37ffe12096a1da263b028df48a8d2909baa3.tar.xz serenity-026a37ffe12096a1da263b028df48a8d2909baa3.zip | |
Add checks for groups
Diffstat (limited to 'src/framework/standard')
| -rw-r--r-- | src/framework/standard/command.rs | 24 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 15 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 10 |
3 files changed, 45 insertions, 4 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 0fa51f9..8d0d33e 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -85,7 +85,6 @@ impl<D: fmt::Display> From<D> for Error { } } -#[derive(Debug)] pub struct CommandGroup { pub prefix: Option<String>, pub commands: HashMap<String, CommandOrAlias>, @@ -98,6 +97,9 @@ pub struct CommandGroup { pub guild_only: bool, pub owners_only: bool, pub help: Option<Arc<Help>>, + /// A set of checks to be called prior to executing the command-group. The checks + /// will short-circuit on the first check that returns `false`. + pub checks: Vec<Check>, } impl Default for CommandGroup { @@ -113,10 +115,29 @@ impl Default for CommandGroup { owners_only: false, allowed_roles: Vec::new(), help: None, + checks: Vec::new(), } } } +impl fmt::Debug for CommandGroup { + // TODO: add CommandGroup::checks somehow? + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("CommandGroup") + .field("prefix", &self.prefix) + .field("commands", &self.commands) + .field("bucket", &self.bucket) + .field("required_permissions", &self.required_permissions) + .field("allowed_roles", &self.allowed_roles) + .field("help_available", &self.help_available) + .field("dm_only", &self.dm_only) + .field("guild_only", &self.guild_only) + .field("owners_only", &self.owners_only) + .field("help", &self.help) + .finish() + } +} + #[derive(Debug)] pub struct CommandOptions { /// A set of checks to be called prior to executing the command. The checks @@ -253,7 +274,6 @@ impl Default for HelpOptions { } } - lazy_static! { static ref DEFAULT_OPTIONS: Arc<CommandOptions> = Arc::new(CommandOptions::default()); } diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index 18f6402..90219d6 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -1,4 +1,5 @@ pub use super::command::{ + Check, Command, CommandGroup, CommandOptions, @@ -161,4 +162,18 @@ impl CreateGroup { self } + + /// Adds a "check" to a group, which checks whether or not the groups's + /// commands should be called. + /// + /// **Note**: These checks are bypassed for commands sent by the application owner. + pub fn check<F>(mut self, check: F) -> Self + where F: Fn(&mut Context, &Message, &mut Args, &CommandOptions) -> bool + + Send + + Sync + + 'static { + self.0.checks.push(Check::new(check)); + + self + } } diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 435069e..db31e0d 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -488,6 +488,7 @@ impl StandardFramework { mut context: &mut Context, message: &Message, command: &Arc<CommandOptions>, + group: &Arc<CommandGroup>, args: &mut Args, to_check: &str, built: &str) @@ -600,12 +601,17 @@ impl StandardFramework { } } - let all_passed = command + let all_group_checks_passed = group .checks .iter() .all(|check| (check.0)(&mut context, message, args, command)); - if all_passed { + let all_command_checks_passed = command + .checks + .iter() + .all(|check| (check.0)(&mut context, message, args, command)); + + if all_group_checks_passed && all_command_checks_passed { None } else { Some(DispatchError::CheckFailed) |