APRK01 commited on
Commit
c8f3880
Β·
1 Parent(s): 283d024

feat: add clear drops command to wipe WSB messages in channels

Browse files
src/commands/clearDrops.js ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { ChannelType } = require('discord.js');
2
+ const { successEmbed, errorEmbed, warnEmbed } = require('../utils/embeds');
3
+
4
+ module.exports = {
5
+ async execute(client, message, args) {
6
+ // Usage: `clear <channel_id>`
7
+ const channelIdStr = args[0]?.replace(/[<#>]/g, '');
8
+ if (!channelIdStr || !/^\d{17,20}$/.test(channelIdStr)) {
9
+ return message.reply({
10
+ embeds: [errorEmbed('Invalid Channel', 'Usage: `clear <channel_id>`\nExample: `clear 1234567890` or `clear #resources`')],
11
+ });
12
+ }
13
+
14
+ const guild = await client.guilds.fetch(process.env.GUILD_ID);
15
+ const channel = await guild.channels.fetch(channelIdStr).catch(() => null);
16
+
17
+ if (!channel || channel.type !== ChannelType.GuildText) {
18
+ return message.reply({
19
+ embeds: [errorEmbed('Channel Not Found', 'Could not find a valid text channel with that ID.')],
20
+ });
21
+ }
22
+
23
+ await message.reply({
24
+ embeds: [warnEmbed('⏳ Clearing Drops', `Fetching and deleting messages posted by WSB in <#${channel.id}>...\nThis might take a moment.`)],
25
+ });
26
+
27
+ try {
28
+ let deletedCount = 0;
29
+ let lastId = null;
30
+ let fetched;
31
+
32
+ // Loop to fetch and delete messages in batches of 100
33
+ do {
34
+ const fetchOptions = { limit: 100 };
35
+ if (lastId) fetchOptions.before = lastId;
36
+
37
+ fetched = await channel.messages.fetch(fetchOptions);
38
+ if (fetched.size === 0) break;
39
+
40
+ // Filter for messages sent by the bot
41
+ const botMessages = fetched.filter(m => m.author.id === client.user.id);
42
+
43
+ if (botMessages.size > 0) {
44
+ // Bulk delete is faster for messages under 14 days old
45
+ // For safety and to handle old messages, we'll delete one by one if bulk fails
46
+ try {
47
+ await channel.bulkDelete(botMessages, true); // true = filter out 14+ day old ones
48
+ deletedCount += botMessages.size;
49
+ } catch (bulkErr) {
50
+ console.warn('[Clear] Bulk delete failed or partially failed, falling back to manual deletion');
51
+ // Fallback manual deletion for older messages
52
+ for (const msg of botMessages.values()) {
53
+ await msg.delete().catch(() => { });
54
+ deletedCount++;
55
+ }
56
+ }
57
+ }
58
+
59
+ lastId = fetched.last().id;
60
+ } while (fetched.size === 100);
61
+
62
+ await message.reply({
63
+ embeds: [successEmbed('βœ… Clear Complete', `Successfully deleted **${deletedCount}** WSB messages from <#${channel.id}>.`)],
64
+ });
65
+ } catch (err) {
66
+ console.error('[Clear Error]', err);
67
+ await message.reply({
68
+ embeds: [errorEmbed('❌ Clear Failed', `An error occurred while clearing messages: \`${err.message}\``)],
69
+ });
70
+ }
71
+ },
72
+ };
src/events/messageCreate.js CHANGED
@@ -17,6 +17,7 @@ const postRules = require('../commands/postRules');
17
  const postDisclaimer = require('../commands/postDisclaimer');
18
  const applyUpdates = require('../commands/applyUpdates');
19
  const fixPings = require('../commands/fixPings');
 
20
 
21
  const OWNER_ID = process.env.OWNER_ID;
22
 
@@ -115,6 +116,12 @@ module.exports = {
115
  return message.reply({ embeds: [successEmbed('βœ… Removed', `User \`${targetId}\` removed from whitelist`)] });
116
  }
117
 
 
 
 
 
 
 
118
  if (content === 'whitelist') {
119
  const all = stmts.getAllWhitelist.all();
120
  if (all.length === 0) {
@@ -132,7 +139,7 @@ module.exports = {
132
 
133
  // Show help
134
  if (content === 'help') {
135
- const allCommands = [...Object.keys(commands), 'drop', 'whitelist', 'whitelist <id> [limit]', 'unwhitelist <id>'];
136
  const embed = infoEmbed('WSB Commands', [
137
  'All commands are **DM-only** and **Owner-only**.\n',
138
  ...allCommands.map(cmd => `\`${cmd}\``),
@@ -147,8 +154,9 @@ module.exports = {
147
  content.startsWith('backup') || content.startsWith('shutdown') ||
148
  content.startsWith('ticket') || content.startsWith('permission') ||
149
  content.startsWith('post') || content.startsWith('drop') ||
150
- content.startsWith('apply') || content.startsWith('fix')) {
151
- return message.reply({ embeds: [errorEmbed('Unknown Command', `Did you mean one of these?\n${[...Object.keys(commands), 'drop'].map(c => `\`${c}\``).join('\n')}`)] });
 
152
  }
153
  return;
154
  }
 
17
  const postDisclaimer = require('../commands/postDisclaimer');
18
  const applyUpdates = require('../commands/applyUpdates');
19
  const fixPings = require('../commands/fixPings');
20
+ const clearDrops = require('../commands/clearDrops');
21
 
22
  const OWNER_ID = process.env.OWNER_ID;
23
 
 
116
  return message.reply({ embeds: [successEmbed('βœ… Removed', `User \`${targetId}\` removed from whitelist`)] });
117
  }
118
 
119
+ // Clear Drops
120
+ if (content.startsWith('clear ')) {
121
+ const args = content.split(' ').slice(1);
122
+ return clearDrops.execute(client, message, args);
123
+ }
124
+
125
  if (content === 'whitelist') {
126
  const all = stmts.getAllWhitelist.all();
127
  if (all.length === 0) {
 
139
 
140
  // Show help
141
  if (content === 'help') {
142
+ const allCommands = [...Object.keys(commands), 'drop', 'whitelist', 'whitelist <id> [limit]', 'unwhitelist <id>', 'clear <channel_id>'];
143
  const embed = infoEmbed('WSB Commands', [
144
  'All commands are **DM-only** and **Owner-only**.\n',
145
  ...allCommands.map(cmd => `\`${cmd}\``),
 
154
  content.startsWith('backup') || content.startsWith('shutdown') ||
155
  content.startsWith('ticket') || content.startsWith('permission') ||
156
  content.startsWith('post') || content.startsWith('drop') ||
157
+ content.startsWith('apply') || content.startsWith('fix') ||
158
+ content.startsWith('clear')) {
159
+ return message.reply({ embeds: [errorEmbed('Unknown Command', `Did you mean one of these?\n${[...Object.keys(commands), 'drop', 'clear <channel_id>'].map(c => `\`${c}\``).join('\n')}`)] });
160
  }
161
  return;
162
  }