| const { ChannelType } = require('discord.js'); |
| const { successEmbed, errorEmbed, warnEmbed } = require('../utils/embeds'); |
|
|
| module.exports = { |
| async execute(client, message, args) { |
| |
| 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; |
|
|
| |
| do { |
| const fetchOptions = { limit: 100 }; |
| if (lastId) fetchOptions.before = lastId; |
|
|
| fetched = await channel.messages.fetch(fetchOptions); |
| if (fetched.size === 0) break; |
|
|
| |
| const botMessages = fetched.filter(m => m.author.id === client.user.id); |
|
|
| if (botMessages.size > 0) { |
| |
| |
| try { |
| await channel.bulkDelete(botMessages, true); |
| deletedCount += botMessages.size; |
| } catch (bulkErr) { |
| console.warn('[Clear] Bulk delete failed or partially failed, falling back to manual deletion'); |
| |
| 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}\``)], |
| }); |
| } |
| }, |
| }; |
|
|