diff options
| author | Lakelezz <[email protected]> | 2018-09-11 17:37:44 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-09-11 17:37:44 +0200 |
| commit | 75f6516fceb6d8e124f91ae25a10f74f183337ad (patch) | |
| tree | 2d25d78512d279cf6d8f5e4e695399be617adab6 /src | |
| parent | Fix to build with Rust 1.25.0. (diff) | |
| download | serenity-75f6516fceb6d8e124f91ae25a10f74f183337ad.tar.xz serenity-75f6516fceb6d8e124f91ae25a10f74f183337ad.zip | |
Add single group help (#385)
Diffstat (limited to 'src')
| -rw-r--r-- | src/framework/standard/command.rs | 2 | ||||
| -rw-r--r-- | src/framework/standard/create_group.rs | 8 | ||||
| -rw-r--r-- | src/framework/standard/help_commands.rs | 69 |
3 files changed, 65 insertions, 14 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 4d7794b..2b1149e 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -105,6 +105,7 @@ pub struct CommandGroup { /// will short-circuit on the first check that returns `false`. pub checks: Vec<Check>, pub default_command: Option<CommandOrAlias>, + pub description: Option<String>, } impl Default for CommandGroup { @@ -122,6 +123,7 @@ impl Default for CommandGroup { help: None, checks: Vec::new(), default_command: None, + description: None, } } } diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs index 0439f63..3cd4047 100644 --- a/src/framework/standard/create_group.rs +++ b/src/framework/standard/create_group.rs @@ -206,4 +206,12 @@ impl CreateGroup { self } + + /// Sets a description for the group that will be displayed if only + /// one specific group is requested via help. + pub fn desc(mut self, text: &str) -> Self { + self.0.description = Some(text.to_string()); + + self + } } diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index f923db0..f7c6b48 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -479,35 +479,53 @@ fn create_command_group_commands_pair_from_groups<'a, H: BuildHasher>( for group_name in group_names { let group = &groups[&**group_name]; - let commands = remove_aliases(&group.commands); - let mut command_names = commands.keys().collect::<Vec<_>>(); - command_names.sort(); - let mut group_with_cmds = fetch_all_eligible_commands_in_group( - &commands, - &command_names, - &help_options, + let group_with_cmds = create_single_group( + group, + group_name, &msg, + &help_options, ); - group_with_cmds.name = group_name; - - if let Some(ref prefixes) = group.prefixes { - group_with_cmds.prefixes.extend_from_slice(&prefixes); - } - listed_groups.push(group_with_cmds); } listed_groups } +/// Fetches a single group with its commands. +fn create_single_group<'a>( + group: &CommandGroup, + group_name: &'a str, + msg: &Message, + help_options: &'a HelpOptions, +) -> GroupCommandsPair<'a> { +let commands = remove_aliases(&group.commands); + let mut command_names = commands.keys().collect::<Vec<_>>(); + command_names.sort(); + + let mut group_with_cmds = fetch_all_eligible_commands_in_group( + &commands, + &command_names, + &help_options, + &msg, + ); + + group_with_cmds.name = group_name; + + if let Some(ref prefixes) = group.prefixes { + group_with_cmds.prefixes.extend_from_slice(&prefixes); + } + + group_with_cmds +} + /// Iterates over all commands and forges them into a `CustomisedHelpData` /// taking `HelpOptions` into consideration when deciding on whether a command /// shall be picked and in what textual format. pub fn create_customised_help_data<'a, H: BuildHasher>( groups: &'a HashMap<String, Arc<CommandGroup>, H>, - args: &Args, + args: &'a Args, help_options: &'a HelpOptions, msg: &Message, ) -> CustomisedHelpData<'a> { @@ -517,6 +535,29 @@ pub fn create_customised_help_data<'a, H: BuildHasher>( return match fetch_single_command(&groups, &name, &help_options, &msg) { Ok(single_command) => single_command, Err(suggestions) => { + let searched_named_lowercase = name.to_lowercase(); + + for (key, group) in groups { + + if key.to_lowercase() == searched_named_lowercase + || group.prefixes.as_ref() + .map_or(false, |v| v.iter().any(|prefix| + *prefix == searched_named_lowercase)) { + + let mut single_group = create_single_group( + &group, + &key, + &msg, + &help_options + ); + + return CustomisedHelpData::GroupedCommands { + help_description: group.description.clone().unwrap_or_default(), + groups: vec![single_group], + }; + } + } + if suggestions.is_empty() { CustomisedHelpData::NoCommandFound { help_error_message: &help_options.no_help_available_text, |