pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 1 | import Discord, { CommandInteraction, GuildChannel, GuildMember, TextChannel } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 4 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 5 | import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 7 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
| 11 | .setName("purge") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 12 | .setDescription("Bulk deletes messages in a channel") |
| 13 | .addIntegerOption(option => option |
| 14 | .setName("amount") |
| 15 | .setDescription("The amount of messages to delete") |
| 16 | .setRequired(false) |
| 17 | .setMinValue(1) |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 18 | .setMaxValue(100)) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 19 | .addUserOption(option => option.setName("user").setDescription("The user to purge messages from").setRequired(false)) |
| 20 | .addStringOption(option => option.setName("reason").setDescription("The reason for the purge").setRequired(false)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 21 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 22 | const callback = async (interaction: CommandInteraction) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 23 | let user = interaction.options.getMember("user") as GuildMember ?? null |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 24 | let channel = (interaction.channel as GuildChannel) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 25 | if (!(["GUILD_TEXT", "GUILD_NEWS", "GUILD_NEWS_THREAD", "GUILD_PUBLIC_THREAD", "GUILD_PRIVATE_THREAD"].includes(channel.type.toString()))) { |
| 26 | return await interaction.reply({ |
| 27 | embeds: [ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 28 | new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 29 | .setEmoji("CHANNEL.PURGE.RED") |
| 30 | .setTitle("Purge") |
| 31 | .setDescription("You cannot purge this channel") |
| 32 | .setStatus("Danger") |
| 33 | ], |
| 34 | components: [], |
| 35 | ephemeral: true, |
| 36 | }) |
| 37 | } |
| 38 | // TODO:[Modals] Replace this with a modal |
| 39 | if ( !interaction.options.getInteger("amount") ) { |
| 40 | await interaction.reply({ |
| 41 | embeds: [ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 42 | new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 43 | .setEmoji("CHANNEL.PURGE.RED") |
| 44 | .setTitle("Purge") |
| 45 | .setDescription("Select how many messages to delete") |
| 46 | .setStatus("Danger") |
| 47 | ], |
| 48 | components: [], |
| 49 | ephemeral: true, |
| 50 | fetchReply: true |
| 51 | }) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 52 | let deleted = [] as Discord.Message[] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 53 | while (true) { |
| 54 | let m = await interaction.editReply({ |
| 55 | embeds: [ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 56 | new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 57 | .setEmoji("CHANNEL.PURGE.RED") |
| 58 | .setTitle("Purge") |
| 59 | .setDescription("Select how many messages to delete. You can continue clicking until all messages are cleared.") |
| 60 | .setStatus("Danger") |
| 61 | ], |
| 62 | components: [ |
| 63 | new Discord.MessageActionRow().addComponents([ |
| 64 | new Discord.MessageButton() |
| 65 | .setCustomId("1") |
| 66 | .setLabel("1") |
| 67 | .setStyle("SECONDARY"), |
| 68 | new Discord.MessageButton() |
| 69 | .setCustomId("3") |
| 70 | .setLabel("3") |
| 71 | .setStyle("SECONDARY"), |
| 72 | new Discord.MessageButton() |
| 73 | .setCustomId("5") |
| 74 | .setLabel("5") |
| 75 | .setStyle("SECONDARY") |
| 76 | ]), |
| 77 | new Discord.MessageActionRow().addComponents([ |
| 78 | new Discord.MessageButton() |
| 79 | .setCustomId("10") |
| 80 | .setLabel("10") |
| 81 | .setStyle("SECONDARY"), |
| 82 | new Discord.MessageButton() |
| 83 | .setCustomId("25") |
| 84 | .setLabel("25") |
| 85 | .setStyle("SECONDARY"), |
| 86 | new Discord.MessageButton() |
| 87 | .setCustomId("50") |
| 88 | .setLabel("50") |
| 89 | .setStyle("SECONDARY") |
| 90 | ]), |
| 91 | new Discord.MessageActionRow().addComponents([ |
| 92 | new Discord.MessageButton() |
| 93 | .setCustomId("done") |
| 94 | .setLabel("Done") |
| 95 | .setStyle("SUCCESS") |
| 96 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 97 | ]) |
| 98 | ] |
| 99 | }) |
| 100 | let component; |
| 101 | try { |
| 102 | component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000}); |
| 103 | } catch (e) { break; } |
| 104 | component.deferUpdate(); |
| 105 | if (component.customId === "done") break; |
| 106 | let amount; |
| 107 | try { amount = parseInt(component.customId); } catch { break; } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 108 | let messages; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 109 | await (interaction.channel as TextChannel).messages.fetch({limit: amount}).then(async (ms) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 110 | if (user) { |
| 111 | ms = ms.filter(m => m.author.id === user.id) |
| 112 | } |
| 113 | messages = await (channel as TextChannel).bulkDelete(ms, true); |
| 114 | }) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 115 | if (messages) { |
| 116 | deleted = deleted.concat(messages.map(m => m)) |
| 117 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 118 | } |
| 119 | if (deleted.length === 0) return await interaction.editReply({ |
| 120 | embeds: [ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 121 | new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 122 | .setEmoji("CHANNEL.PURGE.RED") |
| 123 | .setTitle("Purge") |
| 124 | .setDescription("No messages were deleted") |
| 125 | .setStatus("Danger") |
| 126 | ], |
| 127 | components: [] |
| 128 | }) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 129 | let attachmentObject; |
| 130 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 131 | // @ts-ignore |
| 132 | const { log, NucleusColors, entry, renderUser, renderChannel } = interaction.user.client.logger |
| 133 | let data = { |
| 134 | meta: { |
| 135 | type: 'channelPurge', |
| 136 | displayName: 'Channel Purged', |
| 137 | calculateType: 'messageDelete', |
| 138 | color: NucleusColors.red, |
| 139 | emoji: "PUNISH.BAN.RED", |
| 140 | timestamp: new Date().getTime() |
| 141 | }, |
| 142 | list: { |
| 143 | id: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 144 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 145 | channel: entry(interaction.channel.id, renderChannel(interaction.channel)), |
| 146 | messagesCleared: entry(deleted.length, deleted.length), |
| 147 | }, |
| 148 | hidden: { |
| 149 | guild: interaction.guild.id |
| 150 | } |
| 151 | } |
| 152 | log(data, interaction.user.client); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 153 | let out = "" |
| 154 | deleted.reverse().forEach(message => { |
| 155 | out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date(message.createdTimestamp).toISOString()}]\n` |
| 156 | let lines = message.content.split("\n") |
| 157 | lines.forEach(line => {out += `> ${line}\n`}) |
| 158 | out += `\n\n` |
| 159 | }) |
| 160 | attachmentObject = { |
| 161 | attachment: Buffer.from(out), |
| 162 | name: `purge-${channel.id}-${Date.now()}.txt`, |
| 163 | description: "Purge log" |
| 164 | } |
| 165 | } catch {} |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 166 | let m = await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 167 | .setEmoji(`CHANNEL.PURGE.GREEN`) |
| 168 | .setTitle(`Purge`) |
| 169 | .setDescription("Messages cleared") |
| 170 | .setStatus("Success") |
| 171 | ], components: [new Discord.MessageActionRow().addComponents([ |
| 172 | new Discord.MessageButton() |
| 173 | .setCustomId("download") |
| 174 | .setLabel("Download transcript") |
| 175 | .setStyle("SUCCESS") |
| 176 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 177 | ])]}) |
| 178 | let component; |
| 179 | try { |
| 180 | component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000}); |
| 181 | } catch {} |
| 182 | if (component && component.customId === "download") { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 183 | interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 184 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 185 | .setTitle(`Purge`) |
| 186 | .setDescription("Uploaded") |
| 187 | .setStatus("Success") |
| 188 | ], components: [], files: [attachmentObject]}) |
| 189 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 190 | interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 191 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 192 | .setTitle(`Purge`) |
| 193 | .setDescription("Messages cleared") |
| 194 | .setStatus("Success") |
| 195 | ], components: []}) |
| 196 | } |
| 197 | return |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 198 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 199 | let confirmation = await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 200 | .setEmoji("CHANNEL.PURGE.RED") |
| 201 | .setTitle("Purge") |
| 202 | .setDescription(keyValueList({ |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 203 | "channel": `<#${channel.id}>`, |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 204 | "amount": interaction.options.getInteger("amount").toString(), |
| 205 | "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}` |
| 206 | })) |
| 207 | .setColor("Danger") |
| 208 | // pluralize("day", interaction.options.getInteger("amount")) |
| 209 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 210 | .send() |
| 211 | if (confirmation.success) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 212 | let messages; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 213 | try { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 214 | if (!user) { |
| 215 | let toDelete = await (interaction.channel as TextChannel).messages.fetch({limit: interaction.options.getInteger("amount")}) |
| 216 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
| 217 | } else { |
| 218 | let toDelete = (await (await (interaction.channel as TextChannel).messages.fetch({limit: 100})) |
| 219 | .filter(m => m.author.id === user.id)).first(interaction.options.getInteger("amount")) |
| 220 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
| 221 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 222 | } catch(e) { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 223 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 224 | .setEmoji("CHANNEL.PURGE.RED") |
| 225 | .setTitle(`Purge`) |
| 226 | .setDescription("Something went wrong and no messages were deleted") |
| 227 | .setStatus("Danger") |
| 228 | ], components: []}) |
| 229 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 230 | let attachmentObject; |
| 231 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 232 | // @ts-ignore |
| 233 | const { log, NucleusColors, entry, renderUser, renderChannel } = interaction.user.client.logger |
| 234 | let data = { |
| 235 | meta: { |
| 236 | type: 'channelPurge', |
| 237 | displayName: 'Channel Purged', |
| 238 | calculateType: 'messageDelete', |
| 239 | color: NucleusColors.red, |
| 240 | emoji: "PUNISH.BAN.RED", |
| 241 | timestamp: new Date().getTime() |
| 242 | }, |
| 243 | list: { |
| 244 | id: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 245 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 246 | channel: entry(interaction.channel.id, renderChannel(interaction.channel)), |
| 247 | messagesCleared: entry(messages.size, messages.size), |
| 248 | }, |
| 249 | hidden: { |
| 250 | guild: interaction.guild.id |
| 251 | } |
| 252 | } |
| 253 | log(data, interaction.user.client); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 254 | let out = "" |
| 255 | messages.reverse().forEach(message => { |
| 256 | out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date(message.createdTimestamp).toISOString()}]\n` |
| 257 | let lines = message.content.split("\n") |
| 258 | lines.forEach(line => {out += `> ${line}\n`}) |
| 259 | out += `\n\n` |
| 260 | }) |
| 261 | attachmentObject = { |
| 262 | attachment: Buffer.from(out), |
| 263 | name: `purge-${channel.id}-${Date.now()}.txt`, |
| 264 | description: `Purge log` |
| 265 | } |
| 266 | } catch {} |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 267 | let m = await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 268 | .setEmoji(`CHANNEL.PURGE.GREEN`) |
| 269 | .setTitle(`Purge`) |
| 270 | .setDescription("Messages cleared") |
| 271 | .setStatus("Success") |
| 272 | ], components: [new Discord.MessageActionRow().addComponents([ |
| 273 | new Discord.MessageButton() |
| 274 | .setCustomId("download") |
| 275 | .setLabel("Download transcript") |
| 276 | .setStyle("SUCCESS") |
| 277 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 278 | ])]}) |
| 279 | let component; |
| 280 | try { |
| 281 | component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000}); |
| 282 | } catch {} |
| 283 | if (component && component.customId === "download") { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 284 | interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 285 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 286 | .setTitle(`Purge`) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 287 | .setDescription("Transcript uploaded above") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 288 | .setStatus("Success") |
| 289 | ], components: [], files: [attachmentObject]}) |
| 290 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 291 | interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 292 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 293 | .setTitle(`Purge`) |
| 294 | .setDescription("Messages cleared") |
| 295 | .setStatus("Success") |
| 296 | ], components: []}) |
| 297 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 298 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 299 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 300 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 301 | .setTitle(`Purge`) |
| 302 | .setDescription("No changes were made") |
| 303 | .setStatus("Success") |
| 304 | ], components: []}) |
| 305 | } |
| 306 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 310 | let member = (interaction.member as GuildMember) |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 311 | let me = (interaction.guild.me as GuildMember) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 312 | // Allow the owner to purge |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 313 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 314 | // Check if the user has manage_messages permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 315 | if (! member.permissions.has("MANAGE_MESSAGES")) throw "You do not have the `manage_messages` permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 316 | // Check if nucleus has the manage_messages permission |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 317 | if (! me.permissions.has("MANAGE_MESSAGES")) throw "I do not have the `manage_messages` permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 318 | // Allow warn |
| 319 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 320 | } |
| 321 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 322 | export { command, callback, check }; |