51 lines
1.4 KiB
Rust

use modules::Module;
use toml::Table;
use std::sync::Arc;
use std::sync::mpsc::Receiver;
use helpers::command::split_command;
use event::Event;
pub struct EchoModule {
prefix: String
}
impl EchoModule {
pub fn new (configuration: &Table) -> Box<Module> {
let prefix = configuration.get("prefix")
.and_then(|value| value.as_str())
.unwrap_or("!echo");
Box::new(EchoModule {
prefix: String::from(prefix)
})
}
}
impl Module for EchoModule {
fn consume_events (&self, receiver: Receiver<Arc<Event>>) {
loop {
match receiver.recv() {
Ok(event) => {
match *event {
Event::Message { ref message } => {
debug!("Received message... {:?}", message.content);
match split_command(&message.content) {
Some((command, argument)) => {
if command == self.prefix {
message.reply(argument);
}
},
_ => {}
}
}
_ => ()
}
}
Err(error) => { error!("Error {:?}", error) }
}
}
}
}