Limit to channel rather than guild, 7 day TTL

This commit is contained in:
Gabriel Simmer 2024-06-20 21:14:55 +01:00
parent 9efafd302a
commit ff72e9d520
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
2 changed files with 16 additions and 13 deletions

View file

@ -1,3 +1,4 @@
# duplikate
Duplicate Discord link monitor
Monitors a Discord server and notifies users if a duplicate link has been posted in the last week in
the same channel.

View file

@ -3,7 +3,7 @@ use std::time::Duration;
use dotenv::dotenv;
use once_cell::sync::Lazy;
use redis::Commands;
use redis::{Commands, SetExpiry, SetOptions};
use regex::Regex;
use serenity::all::{
ChannelId, CreateButton, CreateEmbed, CreateEmbedFooter, CreateMessage, GuildId, ReactionType,
@ -23,11 +23,11 @@ impl EventHandler for Handler {
async fn message_delete(
&self,
_ctx: Context,
_channel_id: ChannelId,
channel_id: ChannelId,
deleted_message_id: MessageId,
guild_id: Option<GuildId>,
_guild_id: Option<GuildId>,
) {
let meta = format!("{}-{}", guild_id.unwrap(), deleted_message_id);
let meta = format!("{}-{}", channel_id.get(), deleted_message_id);
let existing: Result<String, redis::RedisError> =
self.redis.get_connection().unwrap().get(&meta);
@ -67,14 +67,16 @@ impl EventHandler for Handler {
}
}
if links.len() > 0 {
let opts = SetOptions::default()
.with_expiration(SetExpiry::EX(60 * 60 * 24 * 7));
let mut exists = vec![];
let guild_id = msg.guild_id.unwrap();
let channel_id = msg.channel_id.get();
for link in links {
let mut s = DefaultHasher::new();
format!("{}-{}", link, guild_id).hash(&mut s);
format!("{}-{}", link, channel_id).hash(&mut s);
let h = s.finish();
let hash = format!("{:x}", h);
let meta = format!("{}-{}", guild_id, msg.id.get());
let meta = format!("{}-{}", channel_id, msg.id.get());
let existing: Result<String, redis::RedisError> =
self.redis.get_connection().unwrap().get(&hash);
if existing.is_ok() {
@ -84,24 +86,24 @@ impl EventHandler for Handler {
.redis
.get_connection()
.unwrap()
.set(&hash, &msg.link())
.set_options(&hash, &msg.link(), opts.clone())
.unwrap();
let _: () = self
.redis
.get_connection()
.unwrap()
.set(meta, hash)
.set_options(meta, hash, opts)
.unwrap();
}
}
if exists.len() > 0 {
// Links have already been posted, let's tell them
let desc = format!(
"{} have already been posted!",
"{} been posted in the last 7 days in this channel",
if exists.len() > 1 {
"Some of these"
"Some of these have"
} else {
"One of these"
"One of these has"
}
);
let mut fields = vec![];