diff options
| author | Lakelezz <[email protected]> | 2018-07-15 16:49:25 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-07-15 16:49:25 +0200 |
| commit | 305d2008216b5351d9fdd357381027ea42f4740b (patch) | |
| tree | de151f91f3774c60abbdaf6cb30831f77ef8c354 /src/framework | |
| parent | Add checks for groups (#349) (diff) | |
| download | serenity-305d2008216b5351d9fdd357381027ea42f4740b.tar.xz serenity-305d2008216b5351d9fdd357381027ea42f4740b.zip | |
Support multiple prefixes for command-groups (#343)
Diffstat (limited to 'src/framework')
| -rw-r--r-- | src/framework/standard/command.rs | 4 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 36 | ||||
| -rw-r--r-- | src/framework/standard/help_commands.rs | 18 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 32 |
4 files changed, 62 insertions, 28 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 23f2f0c..d15b6b2 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -90,7 +90,7 @@ impl<D: fmt::Display> From<D> for Error { #[derive(Debug)] pub struct CommandGroup { - pub prefix: Option<String>, + pub prefixes: Option<Vec<String>>, pub commands: HashMap<String, CommandOrAlias>, /// Some fields taken from Command pub bucket: Option<String>, @@ -109,7 +109,7 @@ pub struct CommandGroup { impl Default for CommandGroup { fn default() -> CommandGroup { CommandGroup { - prefix: None, + prefixes: None, commands: HashMap::new(), bucket: None, required_permissions: Permissions::empty(), diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index c99b829..91eef78 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -13,8 +13,10 @@ pub use super::{ }; use client::Context; -use model::channel::Message; -use model::Permissions; +use model::{ + channel::Message, + Permissions, +}; use std::sync::Arc; /// Used to create command groups @@ -56,11 +58,15 @@ impl CreateGroup { let cmd = f(self.build_command()).finish(); for n in &cmd.options().aliases { - if let Some(ref prefix) = self.0.prefix { - self.0.commands.insert( - format!("{} {}", prefix, n.to_string()), - CommandOrAlias::Alias(format!("{} {}", prefix, command_name.to_string())), - ); + + if let Some(ref prefixes) = self.0.prefixes { + + for prefix in prefixes { + self.0.commands.insert( + format!("{} {}", prefix, n.to_string()), + CommandOrAlias::Alias(format!("{} {}", prefix, command_name.to_string())), + ); + } } else { self.0.commands.insert( n.to_string(), @@ -106,8 +112,20 @@ impl CreateGroup { /// **Note**: serenity automatically puts a space after group prefix. /// /// **Note**: It's suggested to call this first when making a group. - pub fn prefix(mut self, desc: &str) -> Self { - self.0.prefix = Some(desc.to_string()); + pub fn prefix(mut self, prefix: &str) -> Self { + self.0.prefixes = Some(vec![prefix.to_string()]); + + self + } + + /// Sets prefixes to respond to. Each can be a string slice of any + /// non-zero length. + /// + /// **Note**: serenity automatically puts a space after group prefix. + /// + /// **Note**: It's suggested to call this first when making a group. + pub fn prefixes<T: ToString, I: IntoIterator<Item=T>>(mut self, prefixes: I) -> Self { + self.0.prefixes = Some(prefixes.into_iter().map(|prefix| prefix.to_string()).collect()); self } diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index c01325e..a088ef1 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -159,8 +159,8 @@ pub fn with_embeds<H: BuildHasher>( let mut found: Option<(&String, &InternalCommand)> = None; for (command_name, command) in &group.commands { - let with_prefix = if let Some(ref prefix) = group.prefix { - format!("{} {}", prefix, command_name) + let with_prefix = if let Some(ref prefixes) = group.prefixes { + format!("{} {}", prefixes.join("`, `"), command_name) } else { command_name.to_string() }; @@ -229,7 +229,7 @@ pub fn with_embeds<H: BuildHasher>( } if !command.aliases.is_empty() { - let aliases = command.aliases.join(", "); + let aliases = command.aliases.join("`, `"); embed = embed.field(&help_options.aliases_label, aliases, true); } @@ -283,8 +283,8 @@ pub fn with_embeds<H: BuildHasher>( let group = &groups[group_name]; let mut desc = String::new(); - if let Some(ref x) = group.prefix { - let _ = write!(desc, "{}: `{}`\n", &help_options.group_prefix, x); + if let Some(ref prefixes) = group.prefixes { + let _ = write!(desc, "{}: `{}`\n", &help_options.group_prefix, prefixes.join("`, `")); } let mut has_commands = false; @@ -408,8 +408,8 @@ pub fn plain<H: BuildHasher>( let mut found: Option<(&String, &InternalCommand)> = None; for (command_name, command) in &group.commands { - let with_prefix = if let Some(ref prefix) = group.prefix { - format!("{} {}", prefix, command_name) + let with_prefix = if let Some(ref prefixes) = group.prefixes { + format!("{} {}", prefixes.join("`, `"), command_name) } else { command_name.to_string() }; @@ -592,8 +592,8 @@ pub fn plain<H: BuildHasher>( if !group_help.is_empty() { let _ = write!(result, "**{}:** ", group_name); - if let Some(ref x) = group.prefix { - let _ = write!(result, "({}: `{}`): ", help_options.group_prefix, x); + if let Some(ref prefixes) = group.prefixes { + let _ = write!(result, "({}: `{}`): ", help_options.group_prefix, prefixes.join("`, `")); } result.push_str(&group_help); diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 6eaa83e..42da699 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -718,12 +718,16 @@ impl StandardFramework { let cmd = f(CreateCommand::default()).finish(); let name = command_name.to_string(); - if let Some(ref prefix) = group.prefix { + if let Some(ref prefixes) = group.prefixes { + for v in &cmd.options().aliases { - group.commands.insert( - format!("{} {}", prefix, v), - CommandOrAlias::Alias(format!("{} {}", prefix, name)), - ); + + for prefix in prefixes { + group.commands.insert( + format!("{} {}", prefix, v), + CommandOrAlias::Alias(format!("{} {}", prefix, name)), + ); + } } } else { for v in &cmd.options().aliases { @@ -1019,9 +1023,21 @@ impl Framework for StandardFramework { built = points_to.to_string(); } - let to_check = if let Some(ref prefix) = group.prefix { - if built.starts_with(prefix) && command_length > prefix.len() + 1 { - built[(prefix.len() + 1)..].to_string() + let to_check = if let Some(ref prefixes) = group.prefixes { + // Once `built` starts with a set prefix, + // we want to make sure that all following matching prefixes are longer + // 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 { + prefix.len() + } else { + longest_prefix_len + } + ); + + if longest_matching_prefix_len > 0 { + built[(longest_matching_prefix_len + 1)..].to_string() } else { continue; } |