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