diff options
Diffstat (limited to 'src/ext/framework/help_commands.rs')
| -rw-r--r-- | src/ext/framework/help_commands.rs | 120 |
1 files changed, 74 insertions, 46 deletions
diff --git a/src/ext/framework/help_commands.rs b/src/ext/framework/help_commands.rs index e071be5..230d78a 100644 --- a/src/ext/framework/help_commands.rs +++ b/src/ext/framework/help_commands.rs @@ -1,7 +1,8 @@ use std::collections::HashMap; use std::sync::Arc; use std::fmt::Write; -use super::{Command, CommandGroup}; +use super::command::InternalCommand; +use super::{Command, CommandGroup, CommandOrAlias}; use ::client::Context; use ::model::Message; use ::utils::Colour; @@ -13,6 +14,18 @@ fn error_embed(ctx: &Context, message: &Message, input: &str) { .description(input))); } +fn remove_aliases(cmds: &HashMap<String, CommandOrAlias>) -> HashMap<&String, &InternalCommand> { + let mut result = HashMap::new(); + + for (n, v) in cmds { + if let CommandOrAlias::Command(ref cmd) = *v { + result.insert(n, cmd); + } + } + + result +} + pub fn with_embeds(ctx: &Context, message: &Message, groups: HashMap<String, Arc<CommandGroup>>, @@ -21,21 +34,27 @@ pub fn with_embeds(ctx: &Context, let name = args.join(" "); for (group_name, group) in groups { - let mut found: Option<(&String, &Command)> = None; + let mut found: Option<(&String, &InternalCommand)> = None; - if let Some(ref prefix) = group.prefix { - for (command_name, command) in &group.commands { - if name == format!("{} {}", prefix, command_name) { - found = Some((command_name, command)); - } - } - } else { - for (command_name, command) in &group.commands { - if name == command_name[..] { - found = Some((command_name, command)); + for (command_name, command) in &group.commands { + let with_prefix = if let Some(ref prefix) = group.prefix { + format!("{} {}", prefix, command_name) + } else { + command_name.to_owned() + }; + + if name == with_prefix || name == *command_name { + match *command { + CommandOrAlias::Command(ref cmd) => { + found = Some((command_name, cmd)); + }, + CommandOrAlias::Alias(ref name) => { + error_embed(ctx, message, &format!("Did you mean \"{}\"?", name)); + return Ok(()); + } } } - }; + } if let Some((command_name, command)) = found { if !command.help_available { @@ -49,25 +68,25 @@ pub fn with_embeds(ctx: &Context, let mut embed = e.colour(Colour::rosewater()) .title(command_name); if let Some(ref desc) = command.desc { - embed = embed.field(|f| { - f.name("Description") - .value(desc) - .inline(false) - }); + embed = embed.description(desc); } if let Some(ref usage) = command.usage { - embed = embed.field(|f| { - f.name("Usage") - .value(&format!("{} {}", command_name, usage)) - }); + embed = embed.field(|f| f + .name("Usage") + .value(&format!("`{} {}`", command_name, usage))); + } + + if let Some(ref example) = command.example { + embed = embed.field(|f| f + .name("Sample usage") + .value(&format!("`{} {}`", command_name, example))); } if group_name != "Ungrouped" { - embed = embed.field(|f| { - f.name("Group") - .value(&group_name) - }); + embed = embed.field(|f| f + .name("Group") + .value(&group_name)); } let available = if command.dm_only { @@ -78,10 +97,9 @@ pub fn with_embeds(ctx: &Context, "In DM and guilds" }; - embed = embed.field(|f| { - f.name("Available") - .value(available) - }); + embed = embed.field(|f| f + .name("Available") + .value(available)); embed }) @@ -114,7 +132,7 @@ pub fn with_embeds(ctx: &Context, let mut no_commands = true; - for (n, cmd) in &group.commands { + for (n, cmd) in remove_aliases(&group.commands) { if cmd.help_available { let _ = write!(desc, "`{}`\n", n); @@ -146,19 +164,25 @@ pub fn plain(ctx: &Context, for (group_name, group) in groups { let mut found: Option<(&String, &Command)> = None; - if let Some(ref prefix) = group.prefix { - for (command_name, command) in &group.commands { - if name == format!("{} {}", prefix, command_name) { - found = Some((command_name, command)); - } - } - } else { - for (command_name, command) in &group.commands { - if name == command_name[..] { - found = Some((command_name, command)); + for (command_name, command) in &group.commands { + let with_prefix = if let Some(ref prefix) = group.prefix { + format!("{} {}", prefix, command_name) + } else { + command_name.to_owned() + }; + + if name == with_prefix || name == *command_name { + match *command { + CommandOrAlias::Command(ref cmd) => { + found = Some((command_name, cmd)); + }, + CommandOrAlias::Alias(ref name) => { + let _ = ctx.say(&format!("Did you mean {:?}?", name)); + return Ok(()); + } } } - }; + } if let Some((command_name, command)) = found { if !command.help_available { @@ -173,7 +197,11 @@ pub fn plain(ctx: &Context, } if let Some(ref usage) = command.usage { - let _ = write!(result, "**Usage:** {}\n", usage); + let _ = write!(result, "**Usage:** `{} {}`\n", command_name, usage); + } + + if let Some(ref example) = command.example { + let _ = write!(result, "**Sample usage:** `{} {}`\n", command_name, example); } if group_name != "Ungrouped" { @@ -201,8 +229,8 @@ pub fn plain(ctx: &Context, return Ok(()); } - let mut result = "**Commands**\nTo get help about individual command, pass \ - its name as an argument to this command.\n\n" + let mut result = "**Commands**\nTo get help with an individual command, pass its \ + name as an argument to this command.\n\n" .to_string(); for (group_name, group) in groups { @@ -214,7 +242,7 @@ pub fn plain(ctx: &Context, let mut no_commands = true; - for (n, cmd) in &group.commands { + for (n, cmd) in remove_aliases(&group.commands) { if cmd.help_available { let _ = write!(result, "`{}` ", n); |