diff options
| -rw-r--r-- | examples/05_command_framework/src/main.rs | 34 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 34 |
2 files changed, 47 insertions, 21 deletions
diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs index 9518f8a..a5494f0 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -168,10 +168,12 @@ fn main() { .bucket("complicated") .cmd(commands)) .group("Emoji", |g| g - // Sets a single prefix for a group: - .prefix("emoji") + // Sets multiple prefixes for a group. + // This requires us to call commands in this group + // via `~emoji` (or `~e`) instead of just `~`. + .prefixes(vec!["emoji", "em"]) // Sets a command that will be executed if only a group-prefix was passed. - .default_cmd(dog) + .default_cmd(bird) .command("cat", |c| c .desc("Sends an emoji with a cat.") .batch_known_as(vec!["kitty", "neko"]) // Adds multiple aliases @@ -184,10 +186,10 @@ fn main() { .bucket("emoji") .cmd(dog))) .group("Math", |g| g - // Sets multiple prefixes for a group. - // This requires us to call commands in this group - // via `~math` (or `~m`) instead of just `~`. - .prefixes(vec!["m", "math"]) + // Sets a single prefix for this group. + // So one has to call commands in this group + // via `~math` instead of just `~`. + .prefix("math") .command("multiply", |c| c .known_as("*") // Lets us also call `~math *` instead of just `~math multiply`. .cmd(multiply))) @@ -204,7 +206,7 @@ fn main() { .group("Owner", |g| g // This check applies to every command on this group. // User needs to pass the test for the command to execute. - .check(admin_check) + .check(admin_check) .command("am i admin", |c| c .cmd(am_i_admin)) .guild_only(true) @@ -249,7 +251,7 @@ fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) // A function which acts as a "check", to determine whether to call a command. // -// This check analyses whether a guild member permissions has +// This check analyses whether a guild member permissions has // administrator-permissions. fn admin_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> bool { if let Some(member) = msg.member() { @@ -263,7 +265,7 @@ fn admin_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) } command!(some_long_command(_ctx, msg, args) { - if let Err(why) = msg.channel_id.say(&format!("Arguments: {}", args.full())) { + if let Err(why) = msg.channel_id.say(&format!("Arguments: {:?}", args)) { println!("Error sending message: {:?}", why); } }); @@ -381,3 +383,15 @@ command!(cat(_ctx, msg, _args) { println!("Error sending message: {:?}", why); } }); + +command!(bird(_ctx, msg, args) { + let say_content = if args.is_empty() { + ":bird: can find animals for you.".to_string() + } else { + format!(":bird: could not find animal named: `{}`.", args.full()) + }; + + if let Err(why) = msg.channel_id.say(say_content) { + println!("Error sending message: {:?}", why); + } +}); diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 4aa462e..c448c89 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -1043,8 +1043,9 @@ 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) - && (orginal_round.len() == built.len() || command_length > prefix.len() + 1) { + if prefix.len() > longest_prefix_len + && built.starts_with(prefix) + && (orginal_round.len() == prefix.len() || built.get(prefix.len()..prefix.len() + 1) == Some(" ")) { prefix.len() } else { longest_prefix_len @@ -1064,13 +1065,6 @@ impl Framework for StandardFramework { built.clone() }; - let mut args = { - 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) - }; - let before = self.before.clone(); let after = self.after.clone(); @@ -1079,6 +1073,13 @@ impl Framework for StandardFramework { if let Some(help) = help { let groups = self.groups.clone(); + let mut args = { + 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) + }; + threadpool.execute(move || { if let Some(before) = before { @@ -1098,13 +1099,19 @@ impl Framework for StandardFramework { } } - if !to_check.is_empty() { if let Some(&CommandOrAlias::Command(ref command)) = group.commands.get(&to_check) { let command = Arc::clone(command); + let mut args = { + 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) + }; + if let Some(error) = self.should_fail( &mut context, &message, @@ -1149,10 +1156,15 @@ impl Framework for StandardFramework { if let &Some(CommandOrAlias::Command(ref command)) = &group.default_command { let command = Arc::clone(command); + let mut args = { + let content = to_check; + + Args::new(&content.trim(), &self.configuration.delimiters) + }; threadpool.execute(move || { if let Some(before) = before { - if !(before)(&mut context, &message, &to_check) { + if !(before)(&mut context, &message, &args.full()) { return; } } |