@@ -3,24 +3,11 @@ use std::{
33 sync:: { Arc , OnceLock , Weak } ,
44} ;
55
6- use crate :: {
7- channel:: DiscordChannel ,
8- message:: {
9- author:: { DiscordMessageAuthor , DisplayName } ,
10- DiscordMessage ,
11- } ,
12- snowflake:: Snowflake ,
13- } ;
14- use scope_chat:: reaction:: { MessageReactionType , ReactionEvent , ReactionOperation } ;
15- use serenity:: all:: Reaction ;
16- use serenity:: {
17- all:: { Cache , ChannelId , Context , CreateMessage , EventHandler , GatewayIntents , GetMessages , Http , Message , MessageId , Ready } ,
18- async_trait,
19- } ;
20- use tokio:: sync:: { broadcast, RwLock } ;
216use crate :: message:: reaction:: discord_reaction_to_emoji;
227use atomic_refcell:: AtomicRefCell ;
238use dashmap:: DashMap ;
9+ use scope_chat:: reaction:: { MessageReactionType , ReactionEvent , ReactionOperation } ;
10+ use serenity:: all:: Reaction ;
2411use serenity:: {
2512 all:: {
2613 Cache , CacheHttp , ChannelId , Context , CreateMessage , EventHandler , GatewayIntents , GetMessages , GuildId , Http , Member , Message , MessageId ,
@@ -53,7 +40,7 @@ impl CacheHttp for SerenityClient {
5340#[ derive( Default ) ]
5441pub struct DiscordClient {
5542 channel_message_event_handlers : RwLock < HashMap < ChannelId , Vec < broadcast:: Sender < DiscordMessage > > > > ,
56- channel_reaction_event_handlers : RwLock < HashMap < Snowflake , Vec < broadcast:: Sender < ReactionEvent > > > > ,
43+ channel_reaction_event_handlers : RwLock < HashMap < ChannelId , Vec < broadcast:: Sender < ReactionEvent < Snowflake > > > > > ,
5744 client : OnceLock < SerenityClient > ,
5845 user : OnceLock < Arc < User > > ,
5946 channels : RwLock < HashMap < ChannelId , Arc < DiscordChannel > > > ,
@@ -108,7 +95,7 @@ impl DiscordClient {
10895 self . channel_message_event_handlers . write ( ) . await . entry ( channel) . or_default ( ) . push ( sender) ;
10996 }
11097
111- pub async fn add_channel_reaction_sender ( & self , channel : Snowflake , sender : broadcast:: Sender < ReactionEvent > ) {
98+ pub async fn add_channel_reaction_sender ( & self , channel : ChannelId , sender : broadcast:: Sender < ReactionEvent < Snowflake > > ) {
11299 self . channel_reaction_event_handlers . write ( ) . await . entry ( channel) . or_default ( ) . push ( sender) ;
113100 }
114101
@@ -152,10 +139,10 @@ impl DiscordClient {
152139 Some ( channel_id. message ( self . discord ( ) . http . clone ( ) , message_id) . await . unwrap ( ) )
153140 }
154141
155- async fn send_reaction_operation ( & self , channel_id : Snowflake , message_id : MessageId , operation : ReactionOperation ) {
142+ async fn send_reaction_operation ( & self , channel_id : ChannelId , message_id : MessageId , operation : ReactionOperation ) {
156143 if let Some ( vec) = self . channel_reaction_event_handlers . read ( ) . await . get ( & channel_id) {
157144 for sender in vec {
158- let _ = sender. send ( ( message_id. to_string ( ) , operation. clone ( ) ) ) ;
145+ let _ = sender. send ( ( message_id. into ( ) , operation. clone ( ) ) ) ;
159146 }
160147 }
161148 }
@@ -194,47 +181,43 @@ impl EventHandler for DiscordClient {
194181 }
195182
196183 async fn reaction_add ( & self , _: Context , reaction : Reaction ) {
197- let channel_snowflake = reaction. channel_id . into ( ) ;
198-
199184 let ty = if reaction. burst {
200185 MessageReactionType :: Burst
201186 } else {
202187 MessageReactionType :: Normal
203188 } ;
204189
205190 let emoji = discord_reaction_to_emoji ( & reaction. emoji ) ;
191+ let me_id = self . user . get ( ) . expect ( "User not ready" ) . id ;
206192
207- let operation = if reaction. member . is_none_or ( |member| member. user . id . get ( ) == self . user ( ) . id . parse :: < u64 > ( ) . unwrap ( ) ) {
193+ let operation = if reaction. member . is_none_or ( |member| member. user . id == me_id ) {
208194 ReactionOperation :: AddSelf ( emoji, ty)
209195 } else {
210196 ReactionOperation :: Add ( emoji, ty)
211197 } ;
212198
213- self . send_reaction_operation ( channel_snowflake , reaction. message_id , operation) . await ;
199+ self . send_reaction_operation ( reaction . channel_id , reaction. message_id , operation) . await ;
214200 }
215201
216202 async fn reaction_remove ( & self , _: Context , reaction : Reaction ) {
217- let channel_snowflake = reaction. channel_id . into ( ) ;
218-
219203 let emoji = discord_reaction_to_emoji ( & reaction. emoji ) ;
204+ let me_id = self . user . get ( ) . expect ( "User not ready" ) . id ;
220205
221- let operation = if reaction. member . is_none_or ( |member| member. user . id . get ( ) == self . user ( ) . id . parse :: < u64 > ( ) . unwrap ( ) ) {
206+ let operation = if reaction. member . is_none_or ( |member| member. user . id == me_id ) {
222207 ReactionOperation :: RemoveSelf ( emoji)
223208 } else {
224209 ReactionOperation :: Remove ( emoji)
225210 } ;
226211
227- self . send_reaction_operation ( channel_snowflake , reaction. message_id , operation) . await ;
212+ self . send_reaction_operation ( reaction . channel_id , reaction. message_id , operation) . await ;
228213 }
229214
230215 async fn reaction_remove_all ( & self , _: Context , channel_id : ChannelId , removed_from_message_id : MessageId ) {
231- let channel_snowflake = channel_id. into ( ) ;
232- self . send_reaction_operation ( channel_snowflake, removed_from_message_id, ReactionOperation :: RemoveAll ) . await ;
216+ self . send_reaction_operation ( channel_id, removed_from_message_id, ReactionOperation :: RemoveAll ) . await ;
233217 }
234218
235219 async fn reaction_remove_emoji ( & self , _: Context , removed_reactions : Reaction ) {
236- let channel_snowflake = removed_reactions. channel_id . into ( ) ;
237220 let emoji = discord_reaction_to_emoji ( & removed_reactions. emoji ) ;
238- self . send_reaction_operation ( channel_snowflake , removed_reactions. message_id , ReactionOperation :: RemoveEmoji ( emoji) ) . await ;
221+ self . send_reaction_operation ( removed_reactions . channel_id , removed_reactions. message_id , ReactionOperation :: RemoveEmoji ( emoji) ) . await ;
239222 }
240223}
0 commit comments