Skip to content

Commit

Permalink
Add default-commands for command-groups (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakelezz committed Jul 20, 2018
1 parent 614402f commit 40c8248
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions examples/05_command_framework/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ fn main() {
.group("Emoji", |g| g
// Sets a single prefix for a group:
.prefix("emoji")
// Sets a command that will be executed if only a group-prefix was passed.
.default_cmd(dog)
.command("cat", |c| c
.desc("Sends an emoji with a cat.")
.batch_known_as(vec!["kitty", "neko"]) // Adds multiple aliases
Expand Down
2 changes: 2 additions & 0 deletions src/framework/standard/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -120,6 +121,7 @@ impl Default for CommandGroup {
allowed_roles: Vec::new(),
help: None,
checks: Vec::new(),
default_command: None,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/framework/standard/create_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
44 changes: 37 additions & 7 deletions src/framework/standard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 40c8248

Please sign in to comment.