diff options
| -rw-r--r-- | src/framework/standard/command.rs | 2 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 11 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 40 |
3 files changed, 53 insertions, 0 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index d15b6b2..9e867e1 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -92,6 +92,7 @@ impl<D: fmt::Display> From<D> for Error { pub struct CommandGroup { pub prefixes: Option<Vec<String>>, pub commands: HashMap<String, CommandOrAlias>, + pub default_command: Option<CommandOrAlias>, /// Some fields taken from Command pub bucket: Option<String>, pub required_permissions: Permissions, @@ -111,6 +112,7 @@ impl Default for CommandGroup { CommandGroup { prefixes: None, commands: HashMap::new(), + default_command: None, bucket: None, required_permissions: Permissions::empty(), dm_only: false, diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index 91eef78..270cdda 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -83,6 +83,17 @@ impl CreateGroup { self } + /// Sets the default command for the group. + /// This is run when the group prefix is matched, but no sub-commands are found. + pub fn default_command<F>(mut self, f: F) -> Self + where F: FnOnce(CreateCommand) -> CreateCommand { + let cmd = f(self.build_command()).finish(); + + self.0.default_command = Some(CommandOrAlias::Command(cmd)); + + self + } + /// Adds a command to group with a simplified API. /// You can return Err(From::from(string)) if there's an error. pub fn on(self, name: &str, diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 1d7158a..8615571 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -1117,6 +1117,46 @@ impl Framework for StandardFramework { }); return; + } else if let Some(CommandOrAlias::Command(ref command)) = + group.default_command { + let command = Arc::clone(command); + + if let Some(error) = self.should_fail( + &mut context, + &message, + &command.options(), + &group, + &mut args, + &to_check, + &built, + ) { + if let Some(ref handler) = self.dispatch_error_handler { + handler(context, message, error); + } + return; + } + + threadpool.execute(move || { + if let Some(before) = before { + if !(before)(&mut context, &message, &built) { + return; + } + } + + if !command.before(&mut context, &message) { + return; + } + + let result = command.execute(&mut context, &message, args); + + command.after(&mut context, &message, &result); + + if let Some(after) = after { + (after)(&mut context, &message, &built, result); + } + }); + + return; } } } |