attempt to flesh out send support for channel/message. Currently does not build

This commit is contained in:
Adrian Malacoda 2017-02-15 22:41:52 -06:00
parent d414e65fd9
commit 5b9f1610dd
4 changed files with 39 additions and 26 deletions

View File

@ -1,8 +1,8 @@
use {Channel, User}; use {Channel, User};
#[derive(Debug, Clone)] #[derive(Clone)]
pub enum Event { pub enum Event<'a> {
Message { sender: User, channel: Option<Channel>, content: String }, Message { sender: User<'a>, channel: Option<Channel<'a>>, content: String },
Join { channel: Channel }, Join { channel: Channel<'a> },
Quit { channel: Channel } Quit { channel: Channel<'a> }
} }

View File

@ -78,26 +78,28 @@ impl Tenquestionmarks {
} }
} }
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct Channel { pub struct Channel<'a> {
name: String, name: String,
description: String, description: String,
topic: String topic: String,
send_function: &'a Fn(&Channel, &str)
} }
impl Channel { impl<'a> Channel<'a> {
pub fn send (&self, message: &str) { pub fn send (&self, message: &str) {
println!("send to {:?}: {:?}", self.name, message); (self.send_function)(self, message);
} }
} }
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct User { pub struct User<'a> {
name: String name: String,
send_function: &'a Fn(&User, &str)
} }
impl User { impl<'a> User<'a> {
pub fn send (&self, message: &str) { pub fn send (&self, message: &str) {
println!("send to {:?}: {:?}", self.name, message); (self.send_function)(self, message);
} }
} }

View File

@ -37,10 +37,22 @@ impl Plugin for DiscordPlugin {
match connection.recv_event() { match connection.recv_event() {
Ok(Event::MessageCreate(message)) => { Ok(Event::MessageCreate(message)) => {
let author = User { let author = User {
name: message.author.name name: message.author.name,
send_function: &(|user: &User, message: &str| {
println!("Send to {:?}: {:?}", user.name, message);
})
}; };
sender.send(event::Event::Message { sender: author, content: message.content, channel: None }); 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) });
} }
Ok(_) => {} Ok(_) => {}
Err(discord::Error::Closed(code, body)) => { Err(discord::Error::Closed(code, body)) => {

View File

@ -8,23 +8,22 @@ use User;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use event::Event; use event::Event;
pub struct StdinPlugin { pub struct StdinPlugin {}
user: User
}
impl StdinPlugin { impl StdinPlugin {
pub fn new (configuration: &Table) -> Box<Plugin> { pub fn new (configuration: &Table) -> Box<Plugin> {
Box::new(StdinPlugin { Box::new(StdinPlugin {})
user: User {
name: String::from("Dave")
}
})
} }
} }
impl Plugin for StdinPlugin { impl Plugin for StdinPlugin {
fn produce_events<'a>(&'a self, sender: Sender<Event>) { fn produce_events<'a>(&'a self, sender: Sender<Event>) {
let user = &self.user; let user = User {
name: String::from("Dave"),
send_function: &(|user: &User, message: &str| {
println!("Send to {:?}: {:?}", user.name, message);
})
};
loop { loop {
let mut input = String::new(); let mut input = String::new();