diff options
| author | Lakelezz <[email protected]> | 2018-07-21 01:48:12 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-07-21 01:48:12 +0200 |
| commit | 40c8248d107b3c6cad785502e6d619669aba6431 (patch) | |
| tree | c8553c60b7e9b1367c4d6d51fcbc4e00143cd979 /src/framework/standard | |
| parent | Do not suggest command if no command is actually related to input. (#350) (diff) | |
| download | serenity-40c8248d107b3c6cad785502e6d619669aba6431.tar.xz serenity-40c8248d107b3c6cad785502e6d619669aba6431.zip | |
Add default-commands for command-groups (#351)
Diffstat (limited to 'src/framework/standard')
| -rw-r--r-- | src/framework/standard/command.rs | 2 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 12 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 44 |
3 files changed, 51 insertions, 7 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index d15b6b2..a6a6074 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -104,6 +104,7 @@ pub struct CommandGroup { /// 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>, + pub default_command: Option<CommandOrAlias>, } impl Default for CommandGroup { @@ -120,6 +121,7 @@ impl Default for CommandGroup { allowed_roles: Vec::new(), help: None, checks: Vec::new(), + default_command: None, } } } diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index 91eef78..0439f63 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -194,4 +194,16 @@ impl CreateGroup { self } + + /// Adds a command for a group that will be executed if no command-name + /// has been passed. + pub fn default_cmd<C: Command + 'static>(mut self, c: C) -> Self { + let cmd: Arc<Command> = Arc::new(c); + + self.0.default_command = Some(CommandOrAlias::Command(Arc::clone(&cmd))); + + cmd.init(); + + self + } } diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 1d7158a..495f9be 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -988,9 +988,9 @@ impl Framework for StandardFramework { None => return, }; - 'outer: for position in positions { + 'outer: for (index, position) in positions.iter().enumerate() { let mut built = String::new(); - let round = message.content.chars().skip(position).collect::<String>(); + let round = message.content.chars().skip(*position).collect::<String>(); let mut round = round.trim().split_whitespace(); // Call to `trim` causes the related bug under the main bug #206 - where the whitespace settings are ignored. The fix is implemented as an additional check inside command::positions for i in 0..self.configuration.depth { @@ -1027,15 +1027,18 @@ impl Framework for StandardFramework { // than the last matching one, this prevents picking a wrong prefix, // e.g. "f" instead of "ferris" due to "f" having a lower index in the `Vec`. let longest_matching_prefix_len = prefixes.iter().fold(0, |longest_prefix_len, prefix| - if prefix.len() > longest_prefix_len && built.starts_with(prefix) && command_length > prefix.len() + 1 { + if prefix.len() > longest_prefix_len && built.starts_with(prefix) + && (index + 1 == positions.len() || command_length > prefix.len() + 1) { prefix.len() } else { longest_prefix_len } ); - if longest_matching_prefix_len > 0 { - built[(longest_matching_prefix_len + 1)..].to_string() + if longest_matching_prefix_len == built.len() { + String::new() + } else if longest_matching_prefix_len > 0 { + built[longest_matching_prefix_len + 1..].to_string() } else { continue; } @@ -1044,7 +1047,7 @@ impl Framework for StandardFramework { }; let mut args = { - let content = message.content.chars().skip(position).skip_while(|x| x.is_whitespace()) + let content = message.content.chars().skip(*position).skip_while(|x| x.is_whitespace()) .skip(command_length).collect::<String>(); Args::new(&content.trim(), &self.configuration.delimiters) @@ -1053,7 +1056,34 @@ impl Framework for StandardFramework { let before = self.before.clone(); let after = self.after.clone(); - if to_check == "help" { + if to_check.is_empty() { + + if let Some(CommandOrAlias::Command(ref command)) = &group.default_command { + let command = Arc::clone(command); + + 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; + } + } else if to_check == "help" { let help = self.help.clone(); if let Some(help) = help { |