Compare commits

..

No commits in common. "6af528ff9eb58339b1c828a77a38b4f613b33599" and "9efafd302a7376b3e1d1c2fa2ec794a49f325336" have entirely different histories.

3 changed files with 73 additions and 72 deletions

View file

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

View file

@ -13,46 +13,45 @@
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 [
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
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;
};
@ -79,8 +78,13 @@
};
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, SetExpiry, SetOptions};
use redis::Commands;
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!("{}-{}", channel_id.get(), deleted_message_id);
let meta = format!("{}-{}", guild_id.unwrap(), deleted_message_id);
let existing: Result<String, redis::RedisError> =
self.redis.get_connection().unwrap().get(&meta);
@ -67,16 +67,14 @@ 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 channel_id = msg.channel_id.get();
let guild_id = msg.guild_id.unwrap();
for link in links {
let mut s = DefaultHasher::new();
format!("{}-{}", link, channel_id).hash(&mut s);
format!("{}-{}", link, guild_id).hash(&mut s);
let h = s.finish();
let hash = format!("{:x}", h);
let meta = format!("{}-{}", channel_id, msg.id.get());
let meta = format!("{}-{}", guild_id, msg.id.get());
let existing: Result<String, redis::RedisError> =
self.redis.get_connection().unwrap().get(&hash);
if existing.is_ok() {
@ -86,24 +84,24 @@ impl EventHandler for Handler {
.redis
.get_connection()
.unwrap()
.set_options(&hash, &msg.link(), opts.clone())
.set(&hash, &msg.link())
.unwrap();
let _: () = self
.redis
.get_connection()
.unwrap()
.set_options(meta, hash, opts)
.set(meta, hash)
.unwrap();
}
}
if exists.len() > 0 {
// Links have already been posted, let's tell them
let desc = format!(
"{} been posted in the last 7 days in this channel",
"{} have already been posted!",
if exists.len() > 1 {
"Some of these have"
"Some of these"
} else {
"One of these has"
"One of these"
}
);
let mut fields = vec![];