use crate::commands;
use crate::commands::prefixed::context::PrefixedContext;
use crate::commands::prefixed::{PrefixedCommandGroup, PrefixedCommand};
use crate::state::StateBound;
pub async fn help_command<State>(ctx: PrefixedContext<State>, command_name: Option<String>)
where
State: StateBound,
{
if let Some(command_name) = command_name {
for command in commands::flatten_prefixed(&ctx.handle.commands) {
if command.identifiers().contains(&command_name) {
ctx.send(display_command_help(&ctx.command_prefix, command))
.await
.unwrap();
return;
}
}
ctx.send("No command found with that name.").await.unwrap();
} else {
ctx.send(display_general_help(&ctx)).await.unwrap();
}
}
fn display_general_help<State>(ctx: &PrefixedContext<State>) -> String
where
State: StateBound,
{
let bot_commands = commands::get_prefixed_commands(&ctx.handle.commands);
let bot_groups = commands::get_prefixed_groups(&ctx.handle.commands);
let mut result = String::new();
result.push_str("```\n");
result.push_str(&format!(
"{} v{}\n\n{}\n",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_DESCRIPTION")
));
for command in bot_commands {
result.push_str(&display_command(&ctx.command_prefix, command, 0));
}
for group in bot_groups {
result.push_str(&format!(
"\n\n{}",
display_group(&ctx.command_prefix, group, 0)
));
}
result.push_str("\n```");
result
}
fn display_command<State>(
prefix: &str,
command: &PrefixedCommand<State>,
indentation: usize,
) -> String
where
State: StateBound,
{
let mut result = format!("\n{}{}{}", " ".repeat(indentation), prefix, command.name);
if let Some(summary) = &command.summary {
result.push_str(&format!(" - {}", summary));
}
result
}
fn display_group<State>(prefix: &str, group: &PrefixedCommandGroup<State>, indentation: usize) -> String
where
State: StateBound,
{
let mut result = String::new();
let mut header = format!("{}{}", " ".repeat(indentation), group.name);
if let Some(summary) = &group.summary {
header.push_str(&format!(" - {}", summary));
}
header.push(':');
result.push_str(&header);
let bot_commands = commands::get_prefixed_commands(&group.children);
let bot_groups = commands::get_prefixed_groups(&group.children);
for command in bot_commands {
result.push_str(&display_command(prefix, command, indentation + 2));
}
for group in bot_groups {
result.push_str(&format!(
"\n\n{}",
display_group(prefix, group, indentation + 2)
));
}
result
}
fn display_command_help<State>(prefix: &str, command: &PrefixedCommand<State>) -> String
where
State: StateBound,
{
let mut result = String::from("```\n");
result.push_str(&format!("{}{}", prefix, command.name));
if let Some(summary) = &command.summary {
result.push_str(&format!("\n\n{}", summary));
}
if let Some(description) = &command.description {
result.push_str(&format!("\n\n{}", description));
}
result.push_str("\n```");
result
}