|
|
@@ -15,6 +15,7 @@ use std::collections::HashMap; |
|
|
|
use std::string::String; |
|
|
|
use std::fs::{File, OpenOptions}; |
|
|
|
use std::io::BufReader; |
|
|
|
use std::vec::Vec; |
|
|
|
|
|
|
|
use crate::traits::Command; |
|
|
|
|
|
|
@@ -56,7 +57,12 @@ impl Command for CmdMacro { |
|
|
|
self.help(ctx, msg); |
|
|
|
return; |
|
|
|
} else { |
|
|
|
response.push(self.get(&id, &op.as_str().to_string())); |
|
|
|
let mut macro_msg = self.get(&id, &op.as_str().to_string()); |
|
|
|
if msg.content.chars().count() > op.end() { |
|
|
|
// there's some parameters for the macro |
|
|
|
macro_msg = parse_macro(¯o_msg, &msg.content[op.end()+1..]); |
|
|
|
} |
|
|
|
response.push(¯o_msg); |
|
|
|
} |
|
|
|
if let Err(why) = msg.channel_id.say(&ctx.http, &response.build()) { |
|
|
|
println!("Failed to send macro message {:?}", why); |
|
|
@@ -210,3 +216,29 @@ impl CmdMacro { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fn parse_macro<'a, 'b>(macro_pre: &'a str, macro_params: &'b str) -> String { |
|
|
|
if macro_params.chars().count() == 0 { |
|
|
|
return macro_pre.to_string(); |
|
|
|
} |
|
|
|
let re = RegexBuilder::new(r#"^\s*([A-Za-z0-9]+|".*"|'.*')"#) |
|
|
|
.multi_line(true) |
|
|
|
.dot_matches_new_line(true) |
|
|
|
.case_insensitive(true) |
|
|
|
.build() |
|
|
|
.unwrap(); |
|
|
|
let mut parsed_params = Vec::<String>::new(); |
|
|
|
let mut params_str = macro_params.to_string(); |
|
|
|
// move over macro_params like a tape until out of matches |
|
|
|
while let Some(re_capture) = re.captures(¶ms_str) { |
|
|
|
let re_match = re_capture.get(1).unwrap(); |
|
|
|
parsed_params.push(re_match.as_str().trim_matches('\"').trim_matches('\'').to_string()); |
|
|
|
params_str = params_str[re_match.end()..].to_string(); |
|
|
|
} |
|
|
|
let mut macro_post = macro_pre.to_string(); |
|
|
|
// replace {#} format in macro with given param |
|
|
|
for i in 0..parsed_params.len() { |
|
|
|
macro_post = macro_post.replace(&("{".to_owned()+&i.to_string()+"}"), parsed_params.get(i).unwrap()); |
|
|
|
} |
|
|
|
return macro_post; |
|
|
|
} |