diff options
author | Ashelyn Rose <git@ashen.earth> | 2024-07-08 18:13:12 -0600 |
---|---|---|
committer | Ashelyn Rose <git@ashen.earth> | 2024-07-08 18:13:12 -0600 |
commit | f7058336fbad943b2bcc03716c998abe3fd912ec (patch) | |
tree | 0b112332cdd5617121ba03bac0b603337d7a153d | |
parent | a54f77766246f4ce418447cc4d37295c15065b39 (diff) |
Allow for recognizing message edits
-rw-r--r-- | src/system.rs | 21 |
1 files 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<Mutex<lru::LruCache<Id<MessageMarker>, ()>>>, + pub message_dedup_cache: Arc<Mutex<lru::LruCache<Id<MessageMarker>, 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<MessageMarker>) -> bool { + async fn is_new_message(&self, message_id: Id<MessageMarker>, 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<twilight_model::gateway::payload::incoming::MessageUpdate>, _client: &mut Client) { - // TODO: handle message edits and stuff + async fn handle_message_update(&self, message: Box<twilight_model::gateway::payload::incoming::MessageUpdate>, _client: &mut Client) { + println!("Message updated: {}", message.content.unwrap_or("".to_string())) } } |