use a database :D

This commit is contained in:
2024-12-07 00:21:52 -05:00
parent 9fd85e38fb
commit 500c6a6eee
8 changed files with 112 additions and 65 deletions

View File

@@ -1,7 +1,10 @@
use crate::common::{Error, Data};
use poise::serenity_prelude::*;
use poise::serenity_prelude::{self as serenity, Message, UserId};
use regex::Regex;
use std::time::{Instant, Duration};
pub fn extract_mentions(content: &str) -> Vec<UserId> {
// Define the regex pattern for user mentions
let re = Regex::new(r"<@(\d+)>").unwrap();
@@ -11,3 +14,30 @@ pub fn extract_mentions(content: &str) -> Vec<UserId> {
.filter_map(|cap| cap.get(1).map(|id| id.as_str().parse().unwrap()))
.collect()
}
pub async fn ping_spam_yeller(ctx: &serenity::Context, message: &Message, data: &Data) -> Result<(), Error> {
let mentions = extract_mentions(&message.content);
let mut cooldowns = data.mentions.lock().await;
if mentions.iter()
.filter(|&&id| id != message.author.id)
.any(|mention| cooldowns.get(mention).map(|t| Instant::now().duration_since(*t) < Duration::from_secs(20)).unwrap_or(false))
{
message.reply_ping(ctx, "stop spamming!").await?;
let guild = match message.guild_id {
Some(g) => g,
None => return Ok(()),
};
let mut member = guild.member(ctx, message.author.id).await.unwrap();
member.disable_communication_until_datetime(ctx,
serenity::Timestamp::from_unix_timestamp(serenity::Timestamp::now().unix_timestamp() + 60i64).unwrap()).await?;
}
for mention in mentions {
cooldowns.insert(mention, Instant::now());
}
Ok(())
}