File size: 3,141 Bytes
c8f3880 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | const { ChannelType } = require('discord.js');
const { successEmbed, errorEmbed, warnEmbed } = require('../utils/embeds');
module.exports = {
async execute(client, message, args) {
// Usage: `clear <channel_id>`
const channelIdStr = args[0]?.replace(/[<#>]/g, '');
if (!channelIdStr || !/^\d{17,20}$/.test(channelIdStr)) {
return message.reply({
embeds: [errorEmbed('Invalid Channel', 'Usage: `clear <channel_id>`\nExample: `clear 1234567890` or `clear #resources`')],
});
}
const guild = await client.guilds.fetch(process.env.GUILD_ID);
const channel = await guild.channels.fetch(channelIdStr).catch(() => null);
if (!channel || channel.type !== ChannelType.GuildText) {
return message.reply({
embeds: [errorEmbed('Channel Not Found', 'Could not find a valid text channel with that ID.')],
});
}
await message.reply({
embeds: [warnEmbed('β³ Clearing Drops', `Fetching and deleting messages posted by WSB in <#${channel.id}>...\nThis might take a moment.`)],
});
try {
let deletedCount = 0;
let lastId = null;
let fetched;
// Loop to fetch and delete messages in batches of 100
do {
const fetchOptions = { limit: 100 };
if (lastId) fetchOptions.before = lastId;
fetched = await channel.messages.fetch(fetchOptions);
if (fetched.size === 0) break;
// Filter for messages sent by the bot
const botMessages = fetched.filter(m => m.author.id === client.user.id);
if (botMessages.size > 0) {
// Bulk delete is faster for messages under 14 days old
// For safety and to handle old messages, we'll delete one by one if bulk fails
try {
await channel.bulkDelete(botMessages, true); // true = filter out 14+ day old ones
deletedCount += botMessages.size;
} catch (bulkErr) {
console.warn('[Clear] Bulk delete failed or partially failed, falling back to manual deletion');
// Fallback manual deletion for older messages
for (const msg of botMessages.values()) {
await msg.delete().catch(() => { });
deletedCount++;
}
}
}
lastId = fetched.last().id;
} while (fetched.size === 100);
await message.reply({
embeds: [successEmbed('β
Clear Complete', `Successfully deleted **${deletedCount}** WSB messages from <#${channel.id}>.`)],
});
} catch (err) {
console.error('[Clear Error]', err);
await message.reply({
embeds: [errorEmbed('β Clear Failed', `An error occurred while clearing messages: \`${err.message}\``)],
});
}
},
};
|