Skip to content

Commit

Permalink
block_reaction: avoid rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
getchoo committed Apr 10, 2024
1 parent 4795e15 commit 3503dda
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions src/handlers/event/block_reaction.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
use crate::{config::Config, consts::Colors, Data};

use chrono::Duration;
use eyre::{Context as _, Result};
use eyre::Result;
use log::{debug, trace};
use poise::serenity_prelude::{
Context, CreateEmbed, CreateMessage, Mentionable, Message, Reaction, Timestamp, UserId,
Context, CreateEmbed, CreateMessage, Mentionable, Reaction, Timestamp,
};

async fn log_old_react(
ctx: &Context,
config: &Config,
reactor: &Option<UserId>,
message: &Message,
) -> Result<()> {
async fn log_old_react(ctx: &Context, config: &Config, reaction: &Reaction) -> Result<()> {
let Some(log_channel) = config.discord.channels.log_channel_id else {
debug!("Not logging old reaction; no log channel is set!");
return Ok(());
};

let message_link = reaction
.message_id
.link(reaction.channel_id, reaction.guild_id);

let mut embed = CreateEmbed::new()
.title("Old message reaction!")
.color(Colors::Red);

if let Some(reactor) = reactor {
if let Some(reactor) = reaction.user_id {
embed = embed.description(format!(
"{} just reacted to {}!",
"{} just reacted to {message_link}!",
reactor.mention(),
message.link()
));
} else {
embed = embed.description(format!(
"Someone (or something...) just reacted to {}!",
message.link()
"Someone (or something...) just reacted to {message_link}!"
));
}

Expand All @@ -43,33 +40,25 @@ async fn log_old_react(

pub async fn handle(ctx: &Context, reaction: &Reaction, data: &Data) -> Result<()> {
let reaction_type = reaction.emoji.clone();
let reactor = reaction.user_id;
let message = reaction.message(ctx).await.wrap_err_with(|| {
format!(
"Couldn't get message {} from reaction! We won't be able to check if it's old",
reaction.message_id
)
})?;
let message_id = reaction.message_id;
trace!("Checking if we should block reaction on {message_id}");

let time_sent = message.timestamp.to_utc();
let time_sent = message_id.created_at().to_utc();
let age = Timestamp::now().signed_duration_since(time_sent);
let max_days = Duration::days(data.config.discord.days_to_delete_reaction);

if age >= max_days {
// NOTE: if we for some reason **didn't** get the user_id associated with the reaction,
// this will clear **all** reactions of this type. this is intentional as older reactions
// being removed > harmful reactions being kept
debug!(
"Removing reaction {reaction_type} from message {}",
message.id
message_id
);
message.delete_reaction(ctx, reactor, reaction_type).await?;

log_old_react(ctx, &data.config, &reactor, &message).await?;
reaction.delete(ctx).await?;
log_old_react(ctx, &data.config, reaction).await?;
} else {
trace!(
"Keeping reaction {reaction_type} for message {}",
message.id
message_id
);
}

Expand Down

0 comments on commit 3503dda

Please sign in to comment.