|
|
@@ -8,28 +8,94 @@ use serenity::{ |
|
|
|
extern crate json; |
|
|
|
|
|
|
|
extern crate regex; |
|
|
|
use regex::Regex; |
|
|
|
use regex::{Regex, Match}; |
|
|
|
|
|
|
|
use core::option::Option; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::string::String; |
|
|
|
|
|
|
|
use crate::traits::Command; |
|
|
|
|
|
|
|
pub struct CmdMacro {} |
|
|
|
pub struct CmdMacro { |
|
|
|
format: Regex, |
|
|
|
macros: HashMap<String, String> |
|
|
|
} |
|
|
|
|
|
|
|
impl Command for CmdMacro { |
|
|
|
fn execute(&self, ctx: &Context, msg: &Message) { |
|
|
|
|
|
|
|
fn execute(& mut self, ctx: &Context, msg: &Message) { |
|
|
|
if let Some(parsed) = self.format.captures(&msg.content) { |
|
|
|
if let Some(op) = parsed.get(1) { |
|
|
|
let mut response = MessageBuilder::new(); |
|
|
|
if op.as_str().to_string().to_lowercase() == "add" { |
|
|
|
if self.add(parsed.get(2), parsed.get(3)) { |
|
|
|
response.push("Successfully added macro."); |
|
|
|
} else { |
|
|
|
response.push("Missing macro argument or macro already exists."); |
|
|
|
} |
|
|
|
} else if op.as_str().to_string().to_lowercase() == "remove" { |
|
|
|
if self.remove(parsed.get(2)) { |
|
|
|
response.push("Successfully removed macro."); |
|
|
|
} else { |
|
|
|
response.push("Macro does not exist."); |
|
|
|
} |
|
|
|
} else { |
|
|
|
response.push(self.get(&op.as_str().to_string())); |
|
|
|
} |
|
|
|
if let Err(why) = msg.channel_id.say(&ctx.http, &response.build()) { |
|
|
|
println!("Failed to send macro message {:?}", why); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
let response = MessageBuilder::new() |
|
|
|
.push("Hello World") |
|
|
|
.push("Unexpected failure.") |
|
|
|
.build(); |
|
|
|
msg.channel_id.say(&ctx.http, &response); |
|
|
|
if let Err(why) = msg.channel_id.say(&ctx.http, &response) { |
|
|
|
println!("Failed to send macro failure message {:?}", why); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn valid(&self, ctx: &Context, msg: &Message) -> bool { |
|
|
|
return msg.content == "macro"; |
|
|
|
return self.format.is_match(&msg.content); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl CmdMacro { |
|
|
|
pub fn new() -> CmdMacro{ |
|
|
|
return CmdMacro{}; |
|
|
|
return CmdMacro { |
|
|
|
format: |
|
|
|
Regex::new(r#"^!macro\s+([A-Za-z0-9]+|"[^\s]+"|'[^\s]+')(?:\s+([A-Za-z0-9]+|"[^\s]+"|'[^\s]+')\s+(.+))?"#) |
|
|
|
.unwrap(), |
|
|
|
macros: HashMap::<String, String>::new(), // TODO: load map from JSON |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
fn add<'t>(&mut self, key: Option<Match<'t>>, value: Option<Match<'t>>) -> bool { |
|
|
|
let rk; let rv; |
|
|
|
if let Some(k) = key { |
|
|
|
rk = k; |
|
|
|
if self.macros.get(&key.to_owned()).is_some() { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} else {return false;} |
|
|
|
if let Some(v) = value { |
|
|
|
rv = v; |
|
|
|
} else {return false;} |
|
|
|
self.macros.insert(rk.as_str().to_string(), rv.as_str().to_string()); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
fn remove<'t>(&mut self, key: Option<Match<'t>>) -> bool { |
|
|
|
match key { |
|
|
|
Some(k) => return self.macros.remove(&k.as_str().to_string()).is_some(), |
|
|
|
None => return false, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn get(&self, key: &String) -> String { |
|
|
|
if let Some(s) = self.macros.get(&key.to_owned()) { |
|
|
|
return s.to_string(); |
|
|
|
} |
|
|
|
return "Invalid macro".to_string(); |
|
|
|
} |
|
|
|
} |