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