32 lines
No EOL
1.3 KiB
TypeScript
32 lines
No EOL
1.3 KiB
TypeScript
import { ChatInputCommandInteraction, TextChannel, Collection, Message } from "discord.js";
|
|
import { Config } from "../../systems/config";
|
|
import { polls } from "../../systems/poll";
|
|
import { replyAndDelete } from "../../utils";
|
|
|
|
export async function handlePurge(interaction: ChatInputCommandInteraction): Promise<void> {
|
|
const channelId = Config.get({ section: "channels", key: "poll" });
|
|
const channel = await interaction.client.channels.fetch(channelId) as TextChannel;
|
|
if (!channel) return void replyAndDelete(interaction, "❌ Poll channel not found.");
|
|
|
|
// Clear active poll state
|
|
polls.clear();
|
|
|
|
// Fetch and delete bot messages in bulk (Discord allows bulk delete for messages < 14 days old)
|
|
let deleted = 0;
|
|
let messages: Collection<string, Message>;
|
|
|
|
do {
|
|
messages = await channel.messages.fetch({ limit: 100 });
|
|
const botMessages = messages.filter((m) => m.author.id === interaction.client.user!.id);
|
|
if (botMessages.size === 0) break;
|
|
|
|
if (botMessages.size === 1) {
|
|
await botMessages.first()!.delete();
|
|
} else {
|
|
await channel.bulkDelete(botMessages, true); // true = skip messages older than 14 days
|
|
}
|
|
deleted += botMessages.size;
|
|
} while (messages.size === 100);
|
|
|
|
return void replyAndDelete(interaction, `🗑️ Deleted **${deleted}** bot message(s) from <#${channelId}>.`);
|
|
} |