Improve help with sub commands

This commit is contained in:
hodasemi 2022-05-05 07:29:18 +02:00
parent 07effa2257
commit f2f8f8f394

View file

@ -10,6 +10,7 @@ mod config_handler;
mod player;
use serenity::{
builder::CreateEmbed,
framework::standard::{
macros::{group, help},
Args, CommandGroup, CommandResult, HelpOptions, StandardFramework,
@ -41,6 +42,94 @@ create_settings_section!(
#[commands(ip, list, pause, play, remove, skip, stop, tag)]
struct General;
struct HelpGroup {
name: String,
infos: Vec<HelpInfo>,
}
impl HelpGroup {
fn new(name: &str) -> Self {
Self {
name: name.to_string(),
infos: Vec::new(),
}
}
fn add_info(mut self, info: HelpInfo) -> Self {
self.infos.push(info);
self
}
}
struct HelpInfo {
command: String,
sub_commands: Vec<HelpSubInfo>,
}
impl HelpInfo {
fn new(command: &str) -> Self {
Self {
command: command.to_string(),
sub_commands: Vec::new(),
}
}
fn add_sub(mut self, sub_command: impl Into<HelpSubInfo>) -> Self {
self.sub_commands.push(sub_command.into());
self
}
}
enum HelpSubInfo {
Empty,
SubCommand(String),
}
impl<'a> From<&'a str> for HelpSubInfo {
fn from(s: &'a str) -> Self {
Self::SubCommand(s.to_string())
}
}
impl From<String> for HelpSubInfo {
fn from(s: String) -> Self {
Self::SubCommand(s)
}
}
impl From<Option<String>> for HelpSubInfo {
fn from(o: Option<String>) -> Self {
match o {
Some(s) => Self::SubCommand(s.to_string()),
None => Self::Empty,
}
}
}
fn create_help(embed: &mut CreateEmbed, groups: Vec<HelpGroup>) {
for group in groups {
let mut s = String::new();
for info in group.infos {
s += &format!("`{}`\n", info.command);
for sub in info.sub_commands {
s += &format!(
"> {}\n",
match sub {
HelpSubInfo::Empty => "<empty>".to_string(),
HelpSubInfo::SubCommand(sub_command) => sub_command,
}
)
}
}
embed.field(group.name, s, false);
}
}
#[help]
fn my_help(
context: &mut Context,
@ -54,17 +143,29 @@ fn my_help(
m.embed(|embed| {
embed.colour(help_options.embed_success_colour);
embed.field("General", "ip", true);
embed.field("General", "help", true);
let groups = vec![
HelpGroup::new("General")
.add_info(HelpInfo::new("ip"))
.add_info(HelpInfo::new("help")),
HelpGroup::new("Audio Control")
.add_info(
HelpInfo::new("play")
.add_sub(None)
.add_sub("--local")
.add_sub("--tag <tag name>")
.add_sub("<link>")
.add_sub("<name>"),
)
.add_info(HelpInfo::new("pause"))
.add_info(HelpInfo::new("stop"))
.add_info(HelpInfo::new("skip")),
HelpGroup::new("Data Manipulation")
.add_info(HelpInfo::new("list").add_sub(None).add_sub("--tags"))
.add_info(HelpInfo::new("remove"))
.add_info(HelpInfo::new("tag").add_sub("<tag name>")),
];
embed.field("Audio Control", "play", true);
embed.field("Audio Control", "pause", true);
embed.field("Audio Control", "stop", true);
embed.field("Audio Control", "skip", true);
embed.field("Data Manipulation", "list", true);
embed.field("Data Manipulation", "remove", true);
embed.field("Data Manipulation", "tag", true);
create_help(embed, groups);
embed
});