fix: catch voice text channel messages and purge across all watched channels
Some checks failed
ci/woodpecker/push/build Pipeline failed

This commit is contained in:
2026-07-02 12:52:35 +01:00
parent 2522e8a7a0
commit 881c48685e

View File

@@ -4,10 +4,14 @@ use serenity::all::ActivityData;
use serenity::model::channel::MessageFlags; use serenity::model::channel::MessageFlags;
use serenity::{ use serenity::{
async_trait, async_trait,
builder::{CreateAttachment, CreateMessage}, builder::{CreateAttachment, CreateMessage, GetMessages},
client::Context, client::Context,
model::{ model::{
application::Interaction, channel::Message, gateway::Ready, guild::Guild, id::GuildId, application::Interaction,
channel::Message,
gateway::Ready,
guild::Guild,
id::{GuildId, MessageId},
user::OnlineStatus, user::OnlineStatus,
}, },
}; };
@@ -85,9 +89,27 @@ impl serenity::client::EventHandler for Handler {
None => return, None => return,
}; };
match self.db.is_watched_channel(guild_id, msg.channel_id) { // Check if the channel itself is watched, or if this message is in a
Ok(true) => {} // thread (e.g. a voice text channel) whose parent channel is watched.
_ => return, let is_watched = self
.db
.is_watched_channel(guild_id, msg.channel_id)
.unwrap_or(false);
if !is_watched {
if let Some(ref thread_channel) = msg.thread {
if let Some(parent_id) = thread_channel.parent_id {
if self.db.is_watched_channel(guild_id, parent_id).unwrap_or(false) {
// This message is in a thread whose parent is watched
} else {
return;
}
} else {
return;
}
} else {
return;
}
} }
if let Err(e) = self if let Err(e) = self
@@ -191,6 +213,38 @@ impl Handler {
let _ = msg.delete(&ctx.http).await; let _ = msg.delete(&ctx.http).await;
// Purge this user's messages from ALL watched channels within the past hour.
let watched = match self.db.get_watched_channels(guild_id) {
Ok(channels) => channels,
Err(e) => {
eprintln!("Failed to get watched channels: {:?}", e);
vec![]
}
};
let one_hour_ago = msg.id.get() - (3_600_000 << 22);
for channel_id in watched {
let Ok(batch) = channel_id
.messages(&ctx.http, GetMessages::new().limit(10))
.await
else {
continue;
};
let to_delete: Vec<MessageId> = batch
.iter()
.filter(|m| m.author.id == msg.author.id && m.id.get() >= one_hour_ago)
.map(|m| m.id)
.collect();
for chunk in to_delete.chunks(100) {
if let Err(e) = channel_id.delete_messages(&ctx.http, chunk).await {
eprintln!("Failed to bulk delete in {channel_id}: {e:?}");
}
}
}
let ban_reason = self let ban_reason = self
.db .db
.get_ban_reason(guild_id) .get_ban_reason(guild_id)