properly implement discord channels, dry up code

This commit is contained in:
Adrian Malacoda 2017-05-10 02:13:56 -05:00
parent bb25846566
commit 2c0e5170f4

View File

@ -67,6 +67,40 @@ impl Debug for DiscordMessageSender {
}
}
fn make_channel_object (discord: Arc<Discord>, channel: discord::model::Channel) -> Option<Channel> {
match channel {
discord::model::Channel::Group(group) => {
Some(Channel {
name: group.name.unwrap_or_else(|| String::from("Group channel")),
description: String::from(""),
topic: String::from(""),
sender: Box::new(DiscordMessageSender {
discord: discord,
channel_id: group.channel_id
})
})
},
discord::model::Channel::Private(private_channel) => {
None
},
discord::model::Channel::Public(public_channel) => {
Some(channel_from_public_channel(discord, public_channel))
}
}
}
fn channel_from_public_channel (discord: Arc<Discord>, channel: discord::model::PublicChannel) -> Channel {
Channel {
name: channel.name,
description: String::from(""),
topic: channel.topic.unwrap_or_else(|| String::new()),
sender: Box::new(DiscordMessageSender {
discord: discord,
channel_id: channel.id
})
}
}
impl EventLoop for DiscordModule {
fn run (&self, sender: Box<ExtSender<event::Envelope>>, _: Receiver<Arc<event::Envelope>>) {
let discord = Arc::new(Discord::from_bot_token(&self.token[..]).expect("Discord module: Login failed"));
@ -88,15 +122,7 @@ impl EventLoop for DiscordModule {
info!(" - Joined channel: {}", channel.name);
match sender.send(Envelope::new(event::Event::SelfJoin {
channel: Channel {
name: channel.name,
description: String::from(""),
topic: channel.topic.unwrap_or_else(|| String::from("")),
sender: Box::new(DiscordMessageSender {
discord: discord.clone(),
channel_id: channel.id
})
}
channel: channel_from_public_channel(discord.clone(), channel)
})) {
Err(err) => error!("Error sending selfjoin event: {:?}", err),
Ok(_) => {}
@ -115,20 +141,10 @@ impl EventLoop for DiscordModule {
})
};
let channel = Channel {
name: String::from("channel"),
description: String::from(""),
topic: String::from(""),
sender: Box::new(DiscordMessageSender {
discord: discord.clone(),
channel_id: message.channel_id
})
};
let message = Message {
author: author,
content: message.content,
channel: Some(channel)
channel: discord.get_channel(message.channel_id).ok().and_then(|channel| make_channel_object(discord.clone(), channel))
};
match sender.send(Envelope::new(event::Event::Message { message: message })) {