From 442b617f310011bab2e341aed6a047077623086e Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Wed, 22 Feb 2017 23:40:30 -0600 Subject: [PATCH] now with amazing echobox powers --- Cargo.toml | 1 + src/lib.rs | 1 + src/modules/echobox.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ src/modules/loader.rs | 2 ++ src/modules/mod.rs | 1 + tenquestionmarks.toml | 2 ++ 6 files changed, 69 insertions(+) create mode 100644 src/modules/echobox.rs diff --git a/Cargo.toml b/Cargo.toml index 7f90a22..3a9e85a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ rand = "0.3" log = "0.3.6" env_logger = "0.4.0" pvn = { git = "http://gitlab.monarch-pass.net/malacoda/pvn.git" } +echobox = { git = "http://gitlab.monarch-pass.net/malacoda/echobox.git" } diff --git a/src/lib.rs b/src/lib.rs index 6b2e781..38419e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate crossbeam; extern crate discord; extern crate rand; extern crate pvn; +extern crate echobox; use std::collections::BTreeMap; use toml::Table; diff --git a/src/modules/echobox.rs b/src/modules/echobox.rs new file mode 100644 index 0000000..5649c1b --- /dev/null +++ b/src/modules/echobox.rs @@ -0,0 +1,62 @@ +use modules::Module; +use toml::Table; + +use std::sync::Arc; +use std::sync::mpsc::Receiver; + +use helpers::command::split_command; +use event::Event; +use echobox::Echobox; + +pub struct EchoboxModule { + prefix: String, + file: String +} + +impl EchoboxModule { + pub fn new (configuration: &Table) -> Box { + let prefix = configuration.get("prefix") + .and_then(|value| value.as_str()) + .unwrap_or("?echobox"); + + let file = configuration.get("responses") + .and_then(|value| value.as_str()) + .unwrap_or("echobox.db"); + + Box::new(EchoboxModule { + prefix: String::from(prefix), + file: String::from(file) + }) + } +} + +impl Module for EchoboxModule { + fn consume_events (&self, receiver: Receiver>) { + let echobox = Echobox::with_file(&self.file).unwrap(); + + loop { + match receiver.recv() { + Ok(event) => { + match *event { + Event::Message { ref message } => { + debug!("Received message... {:?}", message.content); + match split_command(&message.content) { + Some((command, in_quote)) => { + if command == self.prefix { + match echobox.echo(in_quote) { + Ok(out_quote) => message.reply(&format!("**Quote #{}:** {}", out_quote.id, out_quote.content)), + Err(error) => { error!("Error from echobox.echo(): {:?}", error) } + } + } + }, + _ => {} + } + } + _ => () + } + } + Err(error) => { error!("Error {:?}", error) } + } + } + } +} diff --git a/src/modules/loader.rs b/src/modules/loader.rs index bb67119..cfd1c7c 100644 --- a/src/modules/loader.rs +++ b/src/modules/loader.rs @@ -11,6 +11,7 @@ use modules::stdin::StdinModule; use modules::echo::EchoModule; use modules::random::RandomModule; use modules::pvn::PvnModule; +use modules::echobox::EchoboxModule; pub struct ModuleLoader { types: BTreeMap<&'static str, fn(&Table) -> Box> @@ -25,6 +26,7 @@ impl ModuleLoader { types.insert("echo", EchoModule::new as fn(&Table) -> Box); types.insert("random", RandomModule::new as fn(&Table) -> Box); types.insert("pvn", PvnModule::new as fn(&Table) -> Box); + types.insert("echobox", EchoboxModule::new as fn(&Table) -> Box); ModuleLoader { types: types } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index f68caf5..5bc87d3 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -4,6 +4,7 @@ pub mod stdin; pub mod echo; pub mod random; pub mod pvn; +pub mod echobox; pub mod loader; diff --git a/tenquestionmarks.toml b/tenquestionmarks.toml index 08a0733..ae6a800 100644 --- a/tenquestionmarks.toml +++ b/tenquestionmarks.toml @@ -46,3 +46,5 @@ prefix = "?chk" responses = ["ack"] [pvn] + +[echobox]