Compare commits

...

2 commits

Author SHA1 Message Date
Gabriel Simmer 6af528ff9e
Format Flake 2024-06-20 21:19:13 +01:00
Gabriel Simmer ff72e9d520
Limit to channel rather than guild, 7 day TTL 2024-06-20 21:14:55 +01:00
3 changed files with 72 additions and 73 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

@ -13,45 +13,46 @@
docker-utils.url = "github:collinarnett/docker-utils";
};
outputs = { self, nixpkgs, crane, flake-utils, docker-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
outputs = {
self,
nixpkgs,
crane,
flake-utils,
docker-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
craneLib = crane.mkLib pkgs;
# Common arguments can be set here to avoid repeating them later
# Note: changes here will rebuild all dependency crates
commonArgs = {
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
buildInputs = with pkgs;[
buildInputs = with pkgs;
[
openssl
pkg-config
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
]
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];
};
my-crate = craneLib.buildPackage (commonArgs // {
my-crate = craneLib.buildPackage (commonArgs
// {
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
# Additional environment variables or build phases/hooks can be set
# here *without* rebuilding all dependency crates
# MY_CUSTOM_VAR = "some value";
});
dockerImage = pkgs.dockerTools.buildImage {
name = "git.gmem.ca/arch/duplikate";
tag = "latest-${system}";
config = {
Cmd = [ "${my-crate}/bin/duplikate" ];
Cmd = ["${my-crate}/bin/duplikate"];
};
};
in
{
in {
checks = {
inherit my-crate;
};
@ -78,13 +79,8 @@
};
devShells.default = craneLib.devShell {
# Inherit inputs from checks.
checks = self.checks.${system};
# Additional dev-shell environment variables can be set directly
# MY_CUSTOM_DEVELOPMENT_VAR = "something else";
# Extra inputs can be added here; cargo and rustc are provided by default.
packages = with pkgs; [
rust-analyzer
];

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![];