From 2c0e5170f459a2c4230396daa990dffb44802fa9 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Wed, 10 May 2017 02:13:56 -0500 Subject: [PATCH] properly implement discord channels, dry up code --- src/modules/discord.rs | 56 +++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/modules/discord.rs b/src/modules/discord.rs index e91de70..967d6c1 100644 --- a/src/modules/discord.rs +++ b/src/modules/discord.rs @@ -67,6 +67,40 @@ impl Debug for DiscordMessageSender { } } +fn make_channel_object (discord: Arc, channel: discord::model::Channel) -> Option { + 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, 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>, _: Receiver>) { 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 })) {