|
1 | | -use serenity::all::{ActivityData, Event, Interaction, ReactionType}; |
| 1 | +use std::{ |
| 2 | + collections::HashMap, |
| 3 | + sync::{Arc, LazyLock}, |
| 4 | +}; |
| 5 | + |
| 6 | +use serenity::all::{ActivityData, Event, Interaction, ReactionType, Timestamp, UserId}; |
| 7 | +use tokio::{sync::Mutex, time}; |
2 | 8 |
|
3 | 9 | pub struct EventHandler { |
4 | 10 | pub components: crate::components::ComponentList, |
@@ -44,6 +50,79 @@ impl serenity::all::RawEventHandler for EventHandler { |
44 | 50 | .react(&ctx.http, ReactionType::Unicode('👋'.into())) |
45 | 51 | .await?; |
46 | 52 | } |
| 53 | + |
| 54 | + let Some(guild_id) = event.message.guild_id else { |
| 55 | + return Ok(()); |
| 56 | + }; |
| 57 | + |
| 58 | + for mention in &event.message.mentions { |
| 59 | + if state.env.antimention_user_ids.contains(&mention.id.get()) |
| 60 | + && !state |
| 61 | + .env |
| 62 | + .antimention_user_ids |
| 63 | + .contains(&event.message.author.id.get()) |
| 64 | + && let Ok(mut member) = |
| 65 | + guild_id.member(&ctx.http, event.message.author.id).await |
| 66 | + { |
| 67 | + for role_id in &state.env.antimention_whitelisted_role_ids { |
| 68 | + if member.roles.contains(&(*role_id).into()) { |
| 69 | + return Ok(()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static TIMEOUT_MAP: LazyLock<Arc<Mutex<HashMap<UserId, u32>>>> = |
| 74 | + LazyLock::new(|| { |
| 75 | + let map = Arc::new(Mutex::new(HashMap::new())); |
| 76 | + |
| 77 | + tokio::spawn({ |
| 78 | + let map = Arc::clone(&map); |
| 79 | + async move { |
| 80 | + loop { |
| 81 | + time::sleep(time::Duration::from_hours(6)).await; |
| 82 | + let mut map = map.lock().await; |
| 83 | + map.retain(|_, count| *count > 0); |
| 84 | + for count in map.values_mut() { |
| 85 | + if *count > 0 { |
| 86 | + *count -= 1; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + map |
| 94 | + }); |
| 95 | + |
| 96 | + let mut timeout_map = TIMEOUT_MAP.lock().await; |
| 97 | + let timeout_count = timeout_map.entry(mention.id).or_insert(0); |
| 98 | + *timeout_count += 1; |
| 99 | + let timeout_duration = match *timeout_count { |
| 100 | + 1 | 2 => 30, |
| 101 | + 3 | 4 => 60, |
| 102 | + 5 => 300, |
| 103 | + 6 => 600, |
| 104 | + _ => 3600, |
| 105 | + }; |
| 106 | + drop(timeout_map); |
| 107 | + |
| 108 | + let timestamp = match Timestamp::from_unix_timestamp( |
| 109 | + chrono::Utc::now().timestamp() + timeout_duration, |
| 110 | + ) { |
| 111 | + Ok(t) => t, |
| 112 | + Err(_) => return Ok(()), |
| 113 | + }; |
| 114 | + |
| 115 | + member |
| 116 | + .disable_communication_until(&ctx.http, timestamp) |
| 117 | + .await?; |
| 118 | + event |
| 119 | + .message |
| 120 | + .reply_ping(&ctx.http, "👋 Hey, please do not mention this person. You have been temporarily timed out, repeated offenses will result in longer timeouts.") |
| 121 | + .await?; |
| 122 | + |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
47 | 126 | } |
48 | 127 | Event::InteractionCreate(event) => { |
49 | 128 | if let Interaction::Component(component) = &event.interaction |
|
0 commit comments