From f2f8f8f394c149259259364cc8acd521b9f6b5e7 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 5 May 2022 07:29:18 +0200 Subject: [PATCH] Improve help with sub commands --- src/main.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 111 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4e8a089..e014111 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, +} + +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, +} + +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) -> 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 for HelpSubInfo { + fn from(s: String) -> Self { + Self::SubCommand(s) + } +} + +impl From> for HelpSubInfo { + fn from(o: Option) -> Self { + match o { + Some(s) => Self::SubCommand(s.to_string()), + None => Self::Empty, + } + } +} + +fn create_help(embed: &mut CreateEmbed, groups: Vec) { + 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 => "".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 ") + .add_sub("") + .add_sub(""), + ) + .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("")), + ]; - 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 });