From f7058336fbad943b2bcc03716c998abe3fd912ec Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Mon, 8 Jul 2024 18:13:12 -0600 Subject: Allow for recognizing message edits --- src/system.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/system.rs b/src/system.rs index e707d7f..77df36a 100644 --- a/src/system.rs +++ b/src/system.rs @@ -6,11 +6,12 @@ use tokio::sync::Mutex; use twilight_http::Client; use twilight_model::id::{Id, marker::{MessageMarker, UserMarker}}; use twilight_gateway::{Intents, Shard, ShardId}; +use twilight_model::util::Timestamp; #[derive(Clone)] pub struct System { pub config: crate::config::System, - pub message_dedup_cache: Arc, ()>>>, + pub message_dedup_cache: Arc, Timestamp>>>, } impl System { @@ -64,7 +65,7 @@ impl System { continue } - if self_clone.is_new_message(message.id).await { + if self_clone.is_new_message(message.id, message.timestamp).await { self_clone.handle_message_create(message, &mut client).await; } }, @@ -74,7 +75,7 @@ impl System { continue } - if self_clone.is_new_message(message.id).await { + if self_clone.is_new_message(message.id, message.edited_timestamp.unwrap_or(message.timestamp.expect("No message creation timestamp"))).await { self_clone.handle_message_update(message, &mut client).await; } }, @@ -94,10 +95,14 @@ impl System { } } - async fn is_new_message(&self, message_id: Id) -> bool { + async fn is_new_message(&self, message_id: Id, timestamp: Timestamp) -> bool { let mut message_cache = self.message_dedup_cache.lock().await; - if let None = message_cache.get(&message_id) { - message_cache.put(message_id, ()); + + let last_seen_timestamp = message_cache.get(&message_id); + let current_timestamp = timestamp; + + if last_seen_timestamp.is_none() || last_seen_timestamp.unwrap().as_micros() < current_timestamp.as_micros() { + message_cache.put(message_id, timestamp); true } else { false @@ -115,7 +120,7 @@ impl System { } } - async fn handle_message_update(&self, _message: Box, _client: &mut Client) { - // TODO: handle message edits and stuff + async fn handle_message_update(&self, message: Box, _client: &mut Client) { + println!("Message updated: {}", message.content.unwrap_or("".to_string())) } } -- cgit 1.4.1