Fix voice text channel detection & cross-channel hour purge #1

Merged
HotaruBlaze merged 3 commits from dev into master 2026-07-02 12:59:44 +01:00
Showing only changes of commit 881c48685e - Show all commits

View File

@@ -4,10 +4,14 @@ use serenity::all::ActivityData;
use serenity::model::channel::MessageFlags;
use serenity::{
async_trait,
builder::{CreateAttachment, CreateMessage},
builder::{CreateAttachment, CreateMessage, GetMessages},
client::Context,
model::{
application::Interaction, channel::Message, gateway::Ready, guild::Guild, id::GuildId,
application::Interaction,
channel::Message,
gateway::Ready,
guild::Guild,
id::{GuildId, MessageId},
user::OnlineStatus,
},
};
@@ -85,9 +89,27 @@ impl serenity::client::EventHandler for Handler {
None => return,
};
match self.db.is_watched_channel(guild_id, msg.channel_id) {
Ok(true) => {}
_ => return,
// Check if the channel itself is watched, or if this message is in a
// thread (e.g. a voice text channel) whose parent channel is watched.
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
@@ -191,6 +213,38 @@ impl Handler {
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
.db
.get_ban_reason(guild_id)