add cat and dog modules
This commit is contained in:
parent
3a06d98247
commit
ae9945403f
@ -18,6 +18,8 @@ multimap = "0.4.0"
|
|||||||
notify = "4.0.0"
|
notify = "4.0.0"
|
||||||
irc = "0.13.5"
|
irc = "0.13.5"
|
||||||
thread_local = "0.3"
|
thread_local = "0.3"
|
||||||
pvn = { git = "http://gitlab.monarch-pass.net/malacoda/pvn.git" }
|
pvn = { git = "https://gitlab.monarch-pass.net/malacoda/pvn.git" }
|
||||||
echobox = { git = "http://gitlab.monarch-pass.net/malacoda/echobox.git" }
|
echobox = { git = "https://gitlab.monarch-pass.net/malacoda/echobox.git" }
|
||||||
stc = { git = "http://gitlab.monarch-pass.net/malacoda/stc.git" }
|
stc = { git = "https://gitlab.monarch-pass.net/malacoda/stc.git" }
|
||||||
|
i-can-has-cat = { git = "https://gitlab.monarch-pass.net/malacoda/i-can-has-cat.git" }
|
||||||
|
wow-such-doge = { git = "https://gitlab.monarch-pass.net/malacoda/wow-such-doge.git" }
|
||||||
|
@ -10,6 +10,8 @@ extern crate regex;
|
|||||||
extern crate multimap;
|
extern crate multimap;
|
||||||
extern crate irc;
|
extern crate irc;
|
||||||
extern crate thread_local;
|
extern crate thread_local;
|
||||||
|
extern crate i_can_has_cat;
|
||||||
|
extern crate wow_such_doge;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate hlua;
|
extern crate hlua;
|
||||||
|
62
src/modules/cat.rs
Normal file
62
src/modules/cat.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use modules::EventLoop;
|
||||||
|
use toml::value::Table;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
|
use transformable_channels::mpsc::ExtSender;
|
||||||
|
|
||||||
|
use helpers::command::split_command;
|
||||||
|
use event::{Event, Envelope};
|
||||||
|
use i_can_has_cat::Cats;
|
||||||
|
|
||||||
|
pub struct CatModule {
|
||||||
|
prefix: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CatModule {
|
||||||
|
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
|
||||||
|
let prefix = configuration.get("prefix")
|
||||||
|
.and_then(|value| value.as_str())
|
||||||
|
.unwrap_or("?cat");
|
||||||
|
|
||||||
|
Box::new(CatModule {
|
||||||
|
prefix: String::from(prefix)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventLoop for CatModule {
|
||||||
|
fn run(&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
|
||||||
|
let cats = Cats::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match receiver.recv() {
|
||||||
|
Ok(envelope) => {
|
||||||
|
match envelope.event {
|
||||||
|
Event::Message { ref message } => {
|
||||||
|
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
|
||||||
|
match split_command(&message.content) {
|
||||||
|
Some((command, argument)) => {
|
||||||
|
if command == self.prefix {
|
||||||
|
let cat = if !argument.is_empty() {
|
||||||
|
cats.random_image_of_category(argument)
|
||||||
|
} else {
|
||||||
|
cats.random_image()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Ok(cat) = cat {
|
||||||
|
message.reply(&cat.url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => { error!("Error {:?}", error) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
src/modules/dog.rs
Normal file
62
src/modules/dog.rs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
use modules::EventLoop;
|
||||||
|
use toml::value::Table;
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
|
use transformable_channels::mpsc::ExtSender;
|
||||||
|
|
||||||
|
use helpers::command::split_command;
|
||||||
|
use event::{Event, Envelope};
|
||||||
|
use wow_such_doge::Dogs;
|
||||||
|
|
||||||
|
pub struct DogModule {
|
||||||
|
prefix: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DogModule {
|
||||||
|
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
|
||||||
|
let prefix = configuration.get("prefix")
|
||||||
|
.and_then(|value| value.as_str())
|
||||||
|
.unwrap_or("?dog");
|
||||||
|
|
||||||
|
Box::new(DogModule {
|
||||||
|
prefix: String::from(prefix)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventLoop for DogModule {
|
||||||
|
fn run(&self, _: Box<ExtSender<Event>>, receiver: Receiver<Arc<Envelope>>) {
|
||||||
|
let dogs = Dogs::new();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match receiver.recv() {
|
||||||
|
Ok(envelope) => {
|
||||||
|
match envelope.event {
|
||||||
|
Event::Message { ref message } => {
|
||||||
|
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
|
||||||
|
match split_command(&message.content) {
|
||||||
|
Some((command, argument)) => {
|
||||||
|
if command == self.prefix {
|
||||||
|
let dog = if !argument.is_empty() {
|
||||||
|
dogs.random_image_by_breed(argument)
|
||||||
|
} else {
|
||||||
|
dogs.random_image()
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Ok(dog) = dog {
|
||||||
|
message.reply(&dog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => { error!("Error {:?}", error) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,8 @@ use modules::echobox::EchoboxModule;
|
|||||||
use modules::autolink::AutolinkModule;
|
use modules::autolink::AutolinkModule;
|
||||||
use modules::logger::LoggerModule;
|
use modules::logger::LoggerModule;
|
||||||
use modules::irc::IrcHandler;
|
use modules::irc::IrcHandler;
|
||||||
|
use modules::dog::DogModule;
|
||||||
|
use modules::cat::CatModule;
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
@ -35,6 +37,8 @@ impl ModuleLoader {
|
|||||||
types.insert("autolink", AutolinkModule::new as fn(&Table, &Table) -> Box<EventLoop>);
|
types.insert("autolink", AutolinkModule::new as fn(&Table, &Table) -> Box<EventLoop>);
|
||||||
types.insert("logger", LoggerModule::new as fn(&Table, &Table) -> Box<EventLoop>);
|
types.insert("logger", LoggerModule::new as fn(&Table, &Table) -> Box<EventLoop>);
|
||||||
types.insert("irc", IrcHandler::new as fn(&Table, &Table) -> Box<EventLoop>);
|
types.insert("irc", IrcHandler::new as fn(&Table, &Table) -> Box<EventLoop>);
|
||||||
|
types.insert("cat", CatModule::new as fn(&Table, &Table) -> Box<EventLoop>);
|
||||||
|
types.insert("dog", DogModule::new as fn(&Table, &Table) -> Box<EventLoop>);
|
||||||
ModuleLoader {
|
ModuleLoader {
|
||||||
types: types
|
types: types
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ pub mod echobox;
|
|||||||
pub mod autolink;
|
pub mod autolink;
|
||||||
pub mod logger;
|
pub mod logger;
|
||||||
pub mod irc;
|
pub mod irc;
|
||||||
|
pub mod cat;
|
||||||
|
pub mod dog;
|
||||||
|
|
||||||
pub mod loader;
|
pub mod loader;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user