@@ -3,6 +3,9 @@ use std::future::IntoFuture;
33use std:: sync:: { Arc , RwLock } ;
44use std:: time:: Duration ;
55
6+ use anyhow:: Ok ;
7+ use cache:: SeenCache ;
8+ use repost:: add_repost_counts;
69use twilight_gateway:: { Event , EventTypeFlags , Intents , Shard , ShardId , StreamExt as _} ;
710use twilight_http:: Client ;
811use twilight_model:: channel:: message:: { AllowedMentions , MessageFlags } ;
@@ -18,11 +21,13 @@ use crate::{cache::ReplyCache, config::Config};
1821mod cache;
1922mod config;
2023mod pass;
24+ mod repost;
2125
22- struct State {
26+ pub struct State {
2327 config : Config ,
2428 rest : Client ,
2529 replies : RwLock < ReplyCache > ,
30+ seen : RwLock < SeenCache > ,
2631}
2732
2833#[ tokio:: main]
@@ -38,8 +43,12 @@ async fn main() -> Result<(), anyhow::Error> {
3843 Intents :: GUILD_MESSAGES | Intents :: MESSAGE_CONTENT ,
3944 ) ;
4045
46+ // Use config size if it exists, otherwise default to reply cache size
47+ let seen_size = config. seen_cache_size . unwrap_or ( config. reply_cache_size ) ;
48+
4149 let state = Arc :: new ( State {
4250 replies : RwLock :: new ( ReplyCache :: with_capacity ( config. reply_cache_size ) ) ,
51+ seen : RwLock :: new ( SeenCache :: with_capacity ( seen_size) ) ,
4352 config,
4453 rest,
4554 } ) ;
@@ -118,12 +127,23 @@ async fn dispatch_event(state: Arc<State>, event: Event) -> Result<(), anyhow::E
118127 . await ?;
119128
120129 state. replies . write ( ) . unwrap ( ) . insert ( token, reply. id ) ;
130+ if let Some ( new_content) = add_repost_counts ( & state, reply. id , & reply. content ) {
131+ state. rest
132+ . update_message ( reply. channel_id , reply. id )
133+ . content ( Some ( new_content) . as_deref ( ) )
134+ . allowed_mentions ( Some ( & AllowedMentions :: default ( ) ) )
135+ . await ?;
136+ }
121137 }
122138 }
123139 }
124140
125141 // UPDATE: Edit our reply when someone edits a link in/out
126142 Event :: MessageUpdate ( message) => {
143+ // Ignore non-content edits like removing embeds
144+ if message. edited_timestamp == None {
145+ return Ok ( ( ) ) ;
146+ }
127147 let entry = state. replies . read ( ) . unwrap ( ) . get_entry ( message. id ) ;
128148 let Some ( entry) = entry else {
129149 return Ok ( ( ) ) ;
@@ -143,11 +163,12 @@ async fn dispatch_event(state: Arc<State>, event: Event) -> Result<(), anyhow::E
143163 if let CacheEntry :: Filled ( reply_id) = entry {
144164 if !message. content . is_empty ( ) {
145165 if let Some ( content) = Pass :: apply_all ( & state. config . passes , & message. content ) {
166+ let content = add_repost_counts ( & state, reply_id, & content) ;
146167 state
147168 . rest
148169 . update_message ( message. channel_id , reply_id)
149170 . allowed_mentions ( Some ( & AllowedMentions :: default ( ) ) )
150- . content ( Some ( & content) )
171+ . content ( content. as_deref ( ) )
151172 . await ?;
152173 } else {
153174 state
0 commit comments