2017-02-13 22:13:33 -06:00
|
|
|
use discord;
|
|
|
|
use discord::Discord;
|
|
|
|
use discord::model::Event;
|
|
|
|
|
2017-02-08 03:25:03 -06:00
|
|
|
use plugins::Plugin;
|
|
|
|
use toml::Table;
|
|
|
|
|
2017-02-13 22:13:33 -06:00
|
|
|
use event;
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
|
|
|
|
use User;
|
|
|
|
use Channel;
|
2017-02-08 03:25:03 -06:00
|
|
|
|
2017-02-13 22:13:33 -06:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
pub struct DiscordPlugin {
|
|
|
|
token: String
|
2017-02-08 03:25:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiscordPlugin {
|
|
|
|
pub fn new (configuration: &Table) -> Box<Plugin> {
|
2017-02-13 22:13:33 -06:00
|
|
|
let token = configuration.get("token")
|
|
|
|
.and_then(|value| value.as_str())
|
|
|
|
.unwrap_or("");
|
2017-02-08 03:25:03 -06:00
|
|
|
|
2017-02-13 22:13:33 -06:00
|
|
|
Box::new(DiscordPlugin {
|
|
|
|
token: String::from(token)
|
|
|
|
})
|
2017-02-08 03:25:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 22:13:33 -06:00
|
|
|
impl Plugin for DiscordPlugin {
|
|
|
|
fn produce_events<'a>(&'a self, sender: Sender<event::Event>) {
|
|
|
|
let discord = Discord::from_bot_token(&self.token[..]).expect("Login failed");
|
|
|
|
let (mut connection, _) = discord.connect().expect("Connection failed");
|
|
|
|
loop {
|
|
|
|
match connection.recv_event() {
|
|
|
|
Ok(Event::MessageCreate(message)) => {
|
|
|
|
let author = User {
|
2017-02-15 22:41:52 -06:00
|
|
|
name: message.author.name,
|
|
|
|
send_function: &(|user: &User, message: &str| {
|
|
|
|
println!("Send to {:?}: {:?}", user.name, message);
|
|
|
|
})
|
2017-02-13 22:13:33 -06:00
|
|
|
};
|
|
|
|
|
2017-02-15 22:41:52 -06:00
|
|
|
let channel = Channel {
|
|
|
|
name: String::from("channel"),
|
|
|
|
description: String::from(""),
|
|
|
|
topic: String::from(""),
|
|
|
|
send_function: &(|channel: &Channel, message: &str| {
|
|
|
|
println!("Send to {:?}: {:?}", channel.name, message);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
sender.send(event::Event::Message { sender: author, content: message.content, channel: Option::Some(channel) });
|
2017-02-13 22:13:33 -06:00
|
|
|
}
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(discord::Error::Closed(code, body)) => {
|
|
|
|
println!("Gateway closed on us with code {:?}: {}", code, body);
|
|
|
|
break
|
|
|
|
}
|
|
|
|
Err(err) => println!("Receive error: {:?}", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|