Browse Source

Implement help

master
NGnius 4 years ago
parent
commit
8977f6c173
4 changed files with 43 additions and 11 deletions
  1. +1
    -1
      Cargo.toml
  2. +30
    -6
      src/commands/cmd_macro.rs
  3. +10
    -4
      src/main.rs
  4. +2
    -0
      src/traits/command.rs

+ 1
- 1
Cargo.toml View File

@@ -1,6 +1,6 @@
[package]
name = "leo"
version = "0.1.0"
version = "0.2.0"
authors = ["NGnius <ngniusness@gmail.com>"]
edition = "2018"



+ 30
- 6
src/commands/cmd_macro.rs View File

@@ -22,7 +22,8 @@ const MACRO_PATH: &str = "macros.json";

pub struct CmdMacro {
format: Regex,
macros: HashMap<u64,HashMap<String, String>>
help_format: Regex,
macros: HashMap<u64,HashMap<String, String>>,
}

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),


+ 10
- 4
src/main.rs View File

@@ -29,19 +29,25 @@ impl EventHandler for Handler {
let mut data = ctx.data.write();
let commands = data.get_mut::<CommandsKey>().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());
}
}



+ 2
- 0
src/traits/command.rs View File

@@ -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;
}