From 8977f6c17318f80b37143d6adcb353529ae3a2df Mon Sep 17 00:00:00 2001 From: NGnius Date: Mon, 4 May 2020 20:39:47 -0400 Subject: [PATCH] Implement help --- Cargo.toml | 2 +- src/commands/cmd_macro.rs | 36 ++++++++++++++++++++++++++++++------ src/main.rs | 14 ++++++++++---- src/traits/command.rs | 2 ++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb872ec..9eeb5b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leo" -version = "0.1.0" +version = "0.2.0" authors = ["NGnius "] edition = "2018" diff --git a/src/commands/cmd_macro.rs b/src/commands/cmd_macro.rs index 8451383..fea6361 100644 --- a/src/commands/cmd_macro.rs +++ b/src/commands/cmd_macro.rs @@ -22,7 +22,8 @@ const MACRO_PATH: &str = "macros.json"; pub struct CmdMacro { format: Regex, - macros: HashMap> + help_format: Regex, + macros: HashMap>, } impl Command for CmdMacro { @@ -49,8 +50,11 @@ impl Command for CmdMacro { } else { response.push("Macro does not exist."); } - } else if op.as_str().to_string().to_lowercase() == "list"{ + } else if op.as_str().to_string().to_lowercase() == "list" { response.push(self.list(&id)); + } else if op.as_str().to_string().to_lowercase() == "help" { + self.help(ctx, msg); + return; } else { response.push(self.get(&id, &op.as_str().to_string())); } @@ -68,15 +72,27 @@ impl Command for CmdMacro { } } - fn valid(&self, ctx: &Context, msg: &Message) -> bool { - return self.format.is_match(&msg.content); + fn valid(&self, _ctx: &Context, msg: &Message) -> bool { + return self.format.is_match(&msg.content) && !msg.author.bot; + } + + fn help(&self, ctx: &Context, msg: &Message) { + let mut response = MessageBuilder::new(); + response.push("**Use text shortcuts to generate macro text.**\n!macro (short text)\n!macro list\n!macro add (shortcut) (macro)\n!macro remove (shortcut)".to_string()); + if let Err(why) = msg.channel_id.say(&ctx.http, &response.build()) { + println!("Failed to send macro help message {:?}", why); + } + } + + fn valid_help(&self, _ctx: &Context, msg: &Message) -> bool { + return self.help_format.is_match(&msg.content); } } impl CmdMacro { pub fn new() -> CmdMacro{ // load macro map from JSON - let mut file = OpenOptions::new() + let file: File = OpenOptions::new() .read(true) .write(true) .create(true) @@ -92,6 +108,11 @@ impl CmdMacro { .case_insensitive(true) .build() .unwrap(), + help_format: + RegexBuilder::new(r#"^!help\s*macro$"#) + .case_insensitive(true) + .build() + .unwrap(), macros: macros, }; } @@ -178,7 +199,10 @@ impl CmdMacro { .truncate(true) .open(MACRO_PATH) .unwrap(); - file.sync_all(); + match file.sync_all() { + Err(why) => println!("File sync failed ({:?})", why), + Ok(_) => (), + } //let writer = BufWriter::new(file.unwrap()); match serde_json::to_writer_pretty(file, &self.macros) { Err(why) => println!("Macro saving failed ({:?})", why), diff --git a/src/main.rs b/src/main.rs index 10960e7..691d351 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,19 +29,25 @@ impl EventHandler for Handler { let mut data = ctx.data.write(); let commands = data.get_mut::().unwrap(); // handle messages - let mut count = 0; + // check for help first + for cmd in commands.iter_mut() { + if cmd.valid_help(&ctx, &msg) { + cmd.help(&ctx, &msg); + return; // only do help + } + } for cmd in commands.iter_mut() { if cmd.valid(&ctx, &msg) { - count+=1; cmd.execute(&ctx, &msg); + // multiple commands are allowed } } } fn ready(&self, ctx: Context, ready: Ready) { //ctx.online(); - ctx.set_activity(Activity::playing(&("v".to_owned()+crate_version!()+&" (s:".to_owned()+&ctx.shard_id.to_string()+&")".to_owned()))); - println!("Connected as {} (API v{})", ready.user.name, ready.version); + ctx.set_activity(Activity::playing(&format!("v{} (s:{})", crate_version!(), ctx.shard_id.to_string()))); + println!("Connected as {} (API v{}) to {} guild(s)", ready.user.name, ready.version, ready.guilds.len()); } } diff --git a/src/traits/command.rs b/src/traits/command.rs index a94c483..ef2397f 100644 --- a/src/traits/command.rs +++ b/src/traits/command.rs @@ -7,4 +7,6 @@ use serenity::{ pub trait Command: std::marker::Sync + std::marker::Send { fn execute(& mut self, ctx: &Context, msg: &Message); fn valid(&self, ctx: &Context, msg: &Message) -> bool; + fn help(&self, ctx: &Context, msg: &Message); + fn valid_help(&self, ctx: &Context, msg: &Message) -> bool; }