@@ -25,8 +25,10 @@ use diesel::prelude::*;
25
25
use envy;
26
26
use indexmap:: IndexMap ;
27
27
use serde:: Deserialize ;
28
- use serenity:: { model:: prelude:: * , prelude:: * } ;
28
+ use serenity:: { model:: prelude:: * , prelude:: * , utils:: CustomMessage } ;
29
+ use std:: time:: Duration ;
29
30
31
+ const MESSAGE_AGE_MAX : Duration = Duration :: from_secs ( 3600 ) ;
30
32
#[ derive( Deserialize ) ]
31
33
struct Config {
32
34
tags : bool ,
@@ -186,7 +188,7 @@ fn app() -> Result<()> {
186
188
} ) ;
187
189
188
190
let mut client = Client :: new_with_extras ( & config. discord_token , |e| {
189
- e. raw_event_handler ( Events { cmds } ) ;
191
+ e. event_handler ( Events { cmds } ) ;
190
192
e
191
193
} ) ?;
192
194
@@ -221,33 +223,75 @@ fn main() {
221
223
}
222
224
}
223
225
226
+ struct CommandHistory { }
227
+ impl TypeMapKey for CommandHistory {
228
+ type Value = IndexMap < MessageId , MessageId > ;
229
+ }
230
+
224
231
struct Events {
225
232
cmds : Commands ,
226
233
}
227
234
228
- impl RawEventHandler for Events {
229
- fn raw_event ( & self , cx : Context , event : Event ) {
230
- match event {
231
- Event :: Ready ( ev) => {
232
- info ! ( "{} connected to discord" , ev. ready. user. name) ;
233
- ban:: start_unban_thread ( cx) ;
234
- }
235
- Event :: MessageCreate ( ev) => {
236
- self . cmds . execute ( cx, & ev. message ) ;
237
- }
238
- Event :: ReactionAdd ( ev) => {
239
- if let Err ( e) = welcome:: assign_talk_role ( & cx, & ev) {
240
- error ! ( "{}" , e) ;
241
- }
242
- }
243
- Event :: GuildBanRemove ( ev) => {
244
- if let Err ( e) =
245
- ban:: save_unban ( format ! ( "{}" , ev. user. id) , format ! ( "{}" , ev. guild_id) )
246
- {
247
- error ! ( "{}" , e) ;
248
- }
249
- }
250
- _ => ( ) ,
235
+ impl EventHandler for Events {
236
+ fn ready ( & self , cx : Context , ready : Ready ) {
237
+ info ! ( "{} connected to discord" , ready. user. name) ;
238
+
239
+ let mut data = cx. data . write ( ) ;
240
+ data. insert :: < CommandHistory > ( IndexMap :: new ( ) ) ;
241
+ drop ( data) ;
242
+
243
+ ban:: start_cleanup_thread ( cx) ;
244
+ }
245
+
246
+ fn message ( & self , cx : Context , message : Message ) {
247
+ self . cmds . execute ( cx, & message) ;
248
+ }
249
+
250
+ fn message_update (
251
+ & self ,
252
+ cx : Context ,
253
+ _: Option < Message > ,
254
+ _: Option < Message > ,
255
+ ev : MessageUpdateEvent ,
256
+ ) {
257
+ let age = ev. timestamp . and_then ( |create| {
258
+ ev. edited_timestamp
259
+ . and_then ( |edit| edit. signed_duration_since ( create) . to_std ( ) . ok ( ) )
260
+ } ) ;
261
+
262
+ if age. is_some ( ) && age. unwrap ( ) < MESSAGE_AGE_MAX {
263
+ let mut msg = CustomMessage :: new ( ) ;
264
+ msg. id ( ev. id )
265
+ . channel_id ( ev. channel_id )
266
+ . content ( ev. content . unwrap_or_else ( || String :: new ( ) ) ) ;
267
+
268
+ let msg = msg. build ( ) ;
269
+ info ! (
270
+ "sending edited message - {:?} {:?}" ,
271
+ msg. content, msg. author
272
+ ) ;
273
+ self . cmds . execute ( cx, & msg) ;
274
+ }
275
+ }
276
+
277
+ fn message_delete ( & self , cx : Context , channel_id : ChannelId , message_id : MessageId ) {
278
+ let mut data = cx. data . write ( ) ;
279
+ let history = data. get_mut :: < CommandHistory > ( ) . unwrap ( ) ;
280
+ if let Some ( response_id) = history. remove ( & message_id) {
281
+ info ! ( "deleting message: {:?}" , response_id) ;
282
+ let _ = channel_id. delete_message ( & cx, response_id) ;
283
+ }
284
+ }
285
+
286
+ fn reaction_add ( & self , cx : Context , reaction : Reaction ) {
287
+ if let Err ( e) = welcome:: assign_talk_role ( & cx, & reaction) {
288
+ error ! ( "{}" , e) ;
289
+ }
290
+ }
291
+
292
+ fn guild_ban_removal ( & self , _cx : Context , guild_id : GuildId , user : User ) {
293
+ if let Err ( e) = ban:: save_unban ( format ! ( "{}" , user. id) , format ! ( "{}" , guild_id) ) {
294
+ error ! ( "{}" , e) ;
251
295
}
252
296
}
253
297
}
0 commit comments