| 1 | use std::hash::Hash; |
| 2 | |
| 3 | use serenity::all::Message; |
| 4 | use serenity::all::MessageUpdateEvent; |
| 5 | use serenity::all::Ready; |
| 6 | use serenity::async_trait; |
| 7 | use serenity::prelude::*; |
| 8 | |
| 9 | use crate::relay::*; |
| 10 | |
| 11 | pub struct Handler; |
| 12 | |
| 13 | #[async_trait] |
| 14 | impl EventHandler for Handler { |
| 15 | async fn message(&self, ctx: Context, msg: Message) { |
| 16 | // open the shared lock as write |
| 17 | let data = ctx.data.read().await; |
| 18 | let buffer_lock = data.get::<MessageBuffer>().unwrap().clone(); |
| 19 | |
| 20 | println!("author id: {}, cache id: {}", msg.author.name, ctx.cache.current_user().name); |
| 21 | if let Some(_) = msg.webhook_id { |
| 22 | println!("Same discord author as the bot"); |
| 23 | return; |
| 24 | }; |
| 25 | |
| 26 | { |
| 27 | let mut relay_buffer = buffer_lock.write().await; |
| 28 | // create a new message with the received discord message contents |
| 29 | let mut new_message = RelayMessage::default(); |
| 30 | new_message.contents = msg.content; |
| 31 | new_message.direction = RelayDirection::DIS2IRC(msg.channel_id); |
| 32 | new_message.author = msg.author.name; |
| 33 | // check if message is a reply to also relay it |
| 34 | if let Some(reply) = msg.referenced_message { |
| 35 | new_message.message_type = MessageType::ReplyDiscord((reply.content, reply.author.name)); |
| 36 | } |
| 37 | // push the pending message to the relay buffer |
| 38 | relay_buffer.pending_relay_messages.push_back(new_message); |
| 39 | } |
| 40 | { |
| 41 | let notify = data.get::<RelayNotify>().unwrap().clone(); |
| 42 | notify.notify.notify_one(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | async fn message_update(&self, |
| 47 | ctx: Context, |
| 48 | old_if_available: Option<Message>, |
| 49 | new_if_available: Option<Message>, |
| 50 | event: MessageUpdateEvent) |
| 51 | { |
| 52 | // if the original or new message is not available, it doesn't make sense to store it, as the IRC |
| 53 | // side would have no context of the edit. |
| 54 | if let None = old_if_available { return }; |
| 55 | if let None = new_if_available { return }; |
| 56 | |
| 57 | let old = old_if_available.unwrap(); |
| 58 | let new = new_if_available.unwrap(); |
| 59 | |
| 60 | // open the shared lock as write |
| 61 | let data = ctx.data.read().await; |
| 62 | let buffer_lock = data.get::<MessageBuffer>().unwrap().clone(); |
| 63 | |
| 64 | if let Some(_) = old.webhook_id { |
| 65 | println!("Discarding webhook edit."); |
| 66 | return; |
| 67 | }; |
| 68 | |
| 69 | { |
| 70 | let mut relay_buffer = buffer_lock.write().await; |
| 71 | // create a new message with the received discord message contents |
| 72 | let mut new_message = RelayMessage::default(); |
| 73 | new_message.direction = RelayDirection::DIS2IRC(new.channel_id); |
| 74 | new_message.author = new.author.name; |
| 75 | |
| 76 | let edit_message = format!("edited: \"{}\"\r\n\t↪ {}", old.content, new.content); |
| 77 | new_message.contents = edit_message; |
| 78 | |
| 79 | // push the pending message to the relay buffer |
| 80 | relay_buffer.pending_relay_messages.push_back(new_message); |
| 81 | } |
| 82 | { |
| 83 | let notify = data.get::<RelayNotify>().unwrap().clone(); |
| 84 | notify.notify.notify_one(); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | async fn ready(&self, _: Context, ready: Ready) { |
| 90 | println!("{} is connected!", ready.user.name); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |