now with amazing echobox powers

This commit is contained in:
Adrian Malacoda 2017-02-22 23:40:30 -06:00
parent 2c893926c3
commit 442b617f31
6 changed files with 69 additions and 0 deletions

View File

@ -12,3 +12,4 @@ rand = "0.3"
log = "0.3.6" log = "0.3.6"
env_logger = "0.4.0" env_logger = "0.4.0"
pvn = { git = "http://gitlab.monarch-pass.net/malacoda/pvn.git" } pvn = { git = "http://gitlab.monarch-pass.net/malacoda/pvn.git" }
echobox = { git = "http://gitlab.monarch-pass.net/malacoda/echobox.git" }

View File

@ -3,6 +3,7 @@ extern crate crossbeam;
extern crate discord; extern crate discord;
extern crate rand; extern crate rand;
extern crate pvn; extern crate pvn;
extern crate echobox;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use toml::Table; use toml::Table;

62
src/modules/echobox.rs Normal file
View File

@ -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<Module> {
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<Arc<Event>>) {
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) }
}
}
}
}

View File

@ -11,6 +11,7 @@ use modules::stdin::StdinModule;
use modules::echo::EchoModule; use modules::echo::EchoModule;
use modules::random::RandomModule; use modules::random::RandomModule;
use modules::pvn::PvnModule; use modules::pvn::PvnModule;
use modules::echobox::EchoboxModule;
pub struct ModuleLoader { pub struct ModuleLoader {
types: BTreeMap<&'static str, fn(&Table) -> Box<Module>> types: BTreeMap<&'static str, fn(&Table) -> Box<Module>>
@ -25,6 +26,7 @@ impl ModuleLoader {
types.insert("echo", EchoModule::new as fn(&Table) -> Box<Module>); types.insert("echo", EchoModule::new as fn(&Table) -> Box<Module>);
types.insert("random", RandomModule::new as fn(&Table) -> Box<Module>); types.insert("random", RandomModule::new as fn(&Table) -> Box<Module>);
types.insert("pvn", PvnModule::new as fn(&Table) -> Box<Module>); types.insert("pvn", PvnModule::new as fn(&Table) -> Box<Module>);
types.insert("echobox", EchoboxModule::new as fn(&Table) -> Box<Module>);
ModuleLoader { ModuleLoader {
types: types types: types
} }

View File

@ -4,6 +4,7 @@ pub mod stdin;
pub mod echo; pub mod echo;
pub mod random; pub mod random;
pub mod pvn; pub mod pvn;
pub mod echobox;
pub mod loader; pub mod loader;

View File

@ -46,3 +46,5 @@ prefix = "?chk"
responses = ["ack"] responses = ["ack"]
[pvn] [pvn]
[echobox]