From 9c1e384e0c07a1527bf58cb047028faa0d80d677 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Wed, 6 Apr 2022 11:26:38 -0500 Subject: [PATCH] Follow channel mapping on Discord -> IRC --- src/main.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 34af7ba..692faca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,21 +80,25 @@ impl EventHandler for Handler { let nick_bytes = new_nick.len() + 3; // +1 for the space and +2 for the <> let content_limit = 510 - nick_bytes; - let (user_id, sender, members, channel_id, channel, raw_prefix) = { + let (user_id, sender, members, mapping, raw_prefix) = { let data = ctx.data.read().await; let user_id = data.get::().unwrap().to_owned(); let sender = data.get::().unwrap().to_owned(); let members = data.get::().unwrap().to_owned(); - let channel_id = data.get::().unwrap().to_owned(); - let channel = data.get::().unwrap().to_owned(); let raw_prefix = data .get::() .unwrap() .to_owned() .unwrap_or(String::from("++")); + let mapping = data.get::().unwrap().to_owned(); - (user_id, sender, members, channel_id, channel, raw_prefix) + (user_id, sender, members, mapping, raw_prefix) + }; + + let (channel, channel_id) = match mapping.iter().find(|(_, &v)| v == msg.channel_id.0) { + Some((k, v)) => (k.to_owned(), ChannelId::from(*v)), + None => return }; let attachments: Vec = msg.attachments.iter().map(|a| a.url.clone()).collect(); @@ -105,7 +109,7 @@ impl EventHandler for Handler { lines.remove(lines.len() - 1); lines.remove(0); - if user_id != msg.author.id && !msg.author.bot && msg.channel_id == channel_id { + if user_id != msg.author.id && !msg.author.bot { for line in lines { send_irc_message(&sender, &channel, &format!("<{}> {}", new_nick, line)) .await @@ -274,7 +278,7 @@ impl EventHandler for Handler { let should_raw = computed.starts_with(&raw_prefix); - if user_id != msg.author.id && !msg.author.bot && msg.channel_id == channel_id { + if user_id != msg.author.id && !msg.author.bot { if let Some(reply) = msg.referenced_message { let rnick = { if let Some(member) = reply.member { @@ -406,6 +410,7 @@ struct SenderKey; struct MembersKey; struct StringKey; struct OptionStringKey; +struct ChannelMappingKey; impl TypeMapKey for HttpKey { type Value = Arc; @@ -435,6 +440,10 @@ impl TypeMapKey for OptionStringKey { type Value = Option; } +impl TypeMapKey for ChannelMappingKey { + type Value = HashMap; +} + #[cfg(unix)] async fn terminate_signal() { use tokio::signal::unix::{signal, SignalKind}; @@ -500,6 +509,7 @@ async fn main() -> anyhow::Result<()> { data.insert::(channel_id); data.insert::(conf.channel); data.insert::(conf.raw_prefix); + data.insert::(conf.channels); } let webhook = parse_webhook_url(http.clone(), conf.webhook)