PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 1 | import Discord, { CommandInteraction, GuildChannel, GuildMember, TextChannel, ButtonStyle, ButtonBuilder } from "discord.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 2 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 3 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 5 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 6 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | .setName("purge") |
| 12 | .setDescription("Bulk deletes messages in a channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | .addIntegerOption((option) => |
| 14 | option |
| 15 | .setName("amount") |
| 16 | .setDescription("The amount of messages to delete") |
| 17 | .setRequired(false) |
| 18 | .setMinValue(1) |
| 19 | .setMaxValue(100) |
| 20 | ) |
| 21 | .addUserOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 22 | option.setName("user").setDescription("The user to purge messages from").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | ) |
| 24 | .addStringOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 25 | option.setName("reason").setDescription("The reason for the purge").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 27 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 28 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 29 | if (!interaction.guild) return; |
| 30 | const user = (interaction.options.getMember("user") as GuildMember | null); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 31 | const channel = interaction.channel as GuildChannel; |
| 32 | if ( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 33 | !["GUILD_TEXT", "GUILD_NEWS", "GUILD_NEWS_THREAD", "GUILD_PUBLIC_THREAD", "GUILD_PRIVATE_THREAD"].includes( |
| 34 | channel.type.toString() |
| 35 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | ) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 37 | return await interaction.reply({ |
| 38 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 39 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 40 | .setEmoji("CHANNEL.PURGE.RED") |
| 41 | .setTitle("Purge") |
| 42 | .setDescription("You cannot purge this channel") |
| 43 | .setStatus("Danger") |
| 44 | ], |
| 45 | components: [], |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | ephemeral: true |
| 47 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 48 | } |
| 49 | // TODO:[Modals] Replace this with a modal |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 50 | if (!interaction.options.get("amount")) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | await interaction.reply({ |
| 52 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 54 | .setEmoji("CHANNEL.PURGE.RED") |
| 55 | .setTitle("Purge") |
| 56 | .setDescription("Select how many messages to delete") |
| 57 | .setStatus("Danger") |
| 58 | ], |
| 59 | components: [], |
| 60 | ephemeral: true, |
| 61 | fetchReply: true |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | }); |
| 63 | let deleted = [] as Discord.Message[]; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 64 | let timedOut = false; |
| 65 | let amountSelected = false; |
| 66 | while (!timedOut && !amountSelected) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | const m = (await interaction.editReply({ |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 68 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 69 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 70 | .setEmoji("CHANNEL.PURGE.RED") |
| 71 | .setTitle("Purge") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 72 | .setDescription( |
| 73 | "Select how many messages to delete. You can continue clicking until all messages are cleared." |
| 74 | ) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 75 | .setStatus("Danger") |
| 76 | ], |
| 77 | components: [ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 78 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 79 | new Discord.ButtonBuilder().setCustomId("1").setLabel("1").setStyle(ButtonStyle.Secondary), |
| 80 | new Discord.ButtonBuilder().setCustomId("3").setLabel("3").setStyle(ButtonStyle.Secondary), |
| 81 | new Discord.ButtonBuilder().setCustomId("5").setLabel("5").setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | ]), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 83 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 84 | new Discord.ButtonBuilder().setCustomId("10").setLabel("10").setStyle(ButtonStyle.Secondary), |
| 85 | new Discord.ButtonBuilder().setCustomId("25").setLabel("25").setStyle(ButtonStyle.Secondary), |
| 86 | new Discord.ButtonBuilder().setCustomId("50").setLabel("50").setStyle(ButtonStyle.Secondary) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 87 | ]), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 88 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 89 | new Discord.ButtonBuilder() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 90 | .setCustomId("done") |
| 91 | .setLabel("Done") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 92 | .setStyle(ButtonStyle.Success) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 93 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 94 | ]) |
| 95 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | })) as Discord.Message; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 97 | let component; |
| 98 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 99 | component = m.awaitMessageComponent({ |
| 100 | filter: (m) => m.user.id === interaction.user.id, |
| 101 | time: 300000 |
| 102 | }); |
| 103 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 104 | timedOut = true; |
| 105 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 106 | } |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 107 | (await component).deferUpdate(); |
| 108 | if ((await component).customId === "done") { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 109 | amountSelected = true; |
| 110 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 111 | } |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 112 | const amount = parseInt((await component).customId); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 113 | |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 114 | let messages: Discord.Message[] = []; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 115 | await (interaction.channel as TextChannel).messages.fetch({ limit: amount }).then(async (ms) => { |
| 116 | if (user) { |
| 117 | ms = ms.filter((m) => m.author.id === user.id); |
| 118 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 119 | messages = (await (channel as TextChannel).bulkDelete(ms, true)).map(m => m as Discord.Message); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 120 | }); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 121 | deleted = deleted.concat(messages); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 122 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | if (deleted.length === 0) |
| 124 | return await interaction.editReply({ |
| 125 | embeds: [ |
| 126 | new EmojiEmbed() |
| 127 | .setEmoji("CHANNEL.PURGE.RED") |
| 128 | .setTitle("Purge") |
| 129 | .setDescription("No messages were deleted") |
| 130 | .setStatus("Danger") |
| 131 | ], |
| 132 | components: [] |
| 133 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 134 | if (user) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 135 | await client.database.history.create( |
| 136 | "purge", |
| 137 | interaction.guild.id, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 138 | user.user, |
| 139 | interaction.user, |
| 140 | (interaction.options.get("reason")?.value as (string | null)) ?? "*No reason provided*", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | null, |
| 142 | null, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 143 | deleted.length.toString() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 145 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 146 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | const data = { |
| 148 | meta: { |
| 149 | type: "channelPurge", |
| 150 | displayName: "Channel Purged", |
| 151 | calculateType: "messageDelete", |
| 152 | color: NucleusColors.red, |
| 153 | emoji: "PUNISH.BAN.RED", |
| 154 | timestamp: new Date().getTime() |
| 155 | }, |
| 156 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 157 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 158 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 159 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as GuildChannel)), |
| 160 | messagesCleared: entry(deleted.length.toString(), deleted.length.toString()) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 161 | }, |
| 162 | hidden: { |
| 163 | guild: interaction.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 164 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | }; |
| 166 | log(data); |
| 167 | let out = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 168 | deleted.reverse().forEach((message) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 169 | out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${new Date( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 170 | message.createdTimestamp |
| 171 | ).toISOString()}]\n`; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 172 | const lines = message.content.split("\n"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 173 | lines.forEach((line) => { |
| 174 | out += `> ${line}\n`; |
| 175 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 176 | out += "\n\n"; |
| 177 | }); |
| 178 | const attachmentObject = { |
| 179 | attachment: Buffer.from(out), |
| 180 | name: `purge-${channel.id}-${Date.now()}.txt`, |
| 181 | description: "Purge log" |
| 182 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 183 | const m = (await interaction.editReply({ |
| 184 | embeds: [ |
| 185 | new EmojiEmbed() |
| 186 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 187 | .setTitle("Purge") |
| 188 | .setDescription("Messages cleared") |
| 189 | .setStatus("Success") |
| 190 | ], |
| 191 | components: [ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 192 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 193 | new Discord.ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 194 | .setCustomId("download") |
| 195 | .setLabel("Download transcript") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 196 | .setStyle(ButtonStyle.Success) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 197 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 198 | ]) |
| 199 | ] |
| 200 | })) as Discord.Message; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 201 | let component; |
| 202 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 203 | component = await m.awaitMessageComponent({ |
| 204 | filter: (m) => m.user.id === interaction.user.id, |
| 205 | time: 300000 |
| 206 | }); |
| 207 | } catch { |
| 208 | return; |
| 209 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 210 | if (component.customId === "download") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 211 | interaction.editReply({ |
| 212 | embeds: [ |
| 213 | new EmojiEmbed() |
| 214 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 215 | .setTitle("Purge") |
| 216 | .setDescription("Uploaded") |
| 217 | .setStatus("Success") |
| 218 | ], |
| 219 | components: [], |
| 220 | files: [attachmentObject] |
| 221 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 222 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 223 | interaction.editReply({ |
| 224 | embeds: [ |
| 225 | new EmojiEmbed() |
| 226 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 227 | .setTitle("Purge") |
| 228 | .setDescription("Messages cleared") |
| 229 | .setStatus("Success") |
| 230 | ], |
| 231 | components: [] |
| 232 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 233 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 234 | return; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 235 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 236 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 237 | .setEmoji("CHANNEL.PURGE.RED") |
| 238 | .setTitle("Purge") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 239 | .setDescription( |
| 240 | keyValueList({ |
| 241 | channel: `<#${channel.id}>`, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 242 | amount: (interaction.options.get("amount")?.value as number).toString(), |
| 243 | reason: `\n> ${interaction.options.get("reason")?.value ? interaction.options.get("reason")?.value : "*No reason provided*"}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 244 | }) |
| 245 | ) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 246 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 247 | .send(); |
| 248 | if (confirmation.cancelled) return; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 249 | if (confirmation.success) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 250 | let messages; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 251 | try { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 252 | if (!user) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 253 | const toDelete = await (interaction.channel as TextChannel).messages.fetch({ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 254 | limit: interaction.options.get("amount")?.value as number |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 255 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 256 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 257 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 258 | const toDelete = ( |
| 259 | await ( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 260 | await (interaction.channel as TextChannel).messages.fetch({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 261 | limit: 100 |
| 262 | }) |
| 263 | ).filter((m) => m.author.id === user.id) |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 264 | ).first(interaction.options.get("amount")?.value as number); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 265 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 266 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 267 | } catch (e) { |
| 268 | await interaction.editReply({ |
| 269 | embeds: [ |
| 270 | new EmojiEmbed() |
| 271 | .setEmoji("CHANNEL.PURGE.RED") |
| 272 | .setTitle("Purge") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 273 | .setDescription("Something went wrong and no messages were deleted") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 274 | .setStatus("Danger") |
| 275 | ], |
| 276 | components: [] |
| 277 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 278 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 279 | if (!messages) { |
| 280 | await interaction.editReply({ |
| 281 | embeds: [ |
| 282 | new EmojiEmbed() |
| 283 | .setEmoji("CHANNEL.PURGE.RED") |
| 284 | .setTitle("Purge") |
| 285 | .setDescription("No messages could be deleted") |
| 286 | .setStatus("Danger") |
| 287 | ], |
| 288 | components: [] |
| 289 | }); |
| 290 | return; |
| 291 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 292 | if (user) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 293 | await client.database.history.create( |
| 294 | "purge", |
| 295 | interaction.guild.id, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 296 | user.user, |
| 297 | interaction.user, |
| 298 | (interaction.options.get("reason")?.value as (string | null)) ?? "*No reason provided*", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 299 | null, |
| 300 | null, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 301 | messages.size.toString() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 302 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 303 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 304 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 305 | const data = { |
| 306 | meta: { |
| 307 | type: "channelPurge", |
| 308 | displayName: "Channel Purged", |
| 309 | calculateType: "messageDelete", |
| 310 | color: NucleusColors.red, |
| 311 | emoji: "PUNISH.BAN.RED", |
| 312 | timestamp: new Date().getTime() |
| 313 | }, |
| 314 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 315 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 316 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 317 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as GuildChannel)), |
| 318 | messagesCleared: entry(messages.size.toString(), messages.size.toString()) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 319 | }, |
| 320 | hidden: { |
| 321 | guild: interaction.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 322 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 323 | }; |
| 324 | log(data); |
| 325 | let out = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 326 | messages.reverse().forEach((message) => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 327 | if (!message) { |
| 328 | out += "Unknown message\n\n" |
| 329 | } else { |
| 330 | const author = message.author ?? { username: "Unknown", discriminator: "0000", id: "Unknown" }; |
| 331 | out += `${author.username}#${author.discriminator} (${author.id}) [${new Date( |
| 332 | message.createdTimestamp |
| 333 | ).toISOString()}]\n`; |
| 334 | if (message.content) { |
| 335 | const lines = message.content.split("\n"); |
| 336 | lines.forEach((line) => { |
| 337 | out += `> ${line}\n`; |
| 338 | }); |
| 339 | } |
| 340 | if (message.attachments.size > 0) { |
| 341 | message.attachments.forEach((attachment) => { |
| 342 | out += `Attachment > ${attachment.url}\n`; |
| 343 | }); |
| 344 | } |
| 345 | out += "\n\n"; |
| 346 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 347 | }); |
| 348 | const attachmentObject = { |
| 349 | attachment: Buffer.from(out), |
| 350 | name: `purge-${channel.id}-${Date.now()}.txt`, |
| 351 | description: "Purge log" |
| 352 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 353 | const m = (await interaction.editReply({ |
| 354 | embeds: [ |
| 355 | new EmojiEmbed() |
| 356 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 357 | .setTitle("Purge") |
| 358 | .setDescription("Messages cleared") |
| 359 | .setStatus("Success") |
| 360 | ], |
| 361 | components: [ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 362 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 363 | new Discord.ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 364 | .setCustomId("download") |
| 365 | .setLabel("Download transcript") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 366 | .setStyle(ButtonStyle.Success) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 367 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 368 | ]) |
| 369 | ] |
| 370 | })) as Discord.Message; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 371 | let component; |
| 372 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 373 | component = await m.awaitMessageComponent({ |
| 374 | filter: (m) => m.user.id === interaction.user.id, |
| 375 | time: 300000 |
| 376 | }); |
| 377 | } catch { |
| 378 | return; |
| 379 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 380 | if (component.customId === "download") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 381 | interaction.editReply({ |
| 382 | embeds: [ |
| 383 | new EmojiEmbed() |
| 384 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 385 | .setTitle("Purge") |
| 386 | .setDescription("Transcript uploaded above") |
| 387 | .setStatus("Success") |
| 388 | ], |
| 389 | components: [], |
| 390 | files: [attachmentObject] |
| 391 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 392 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 393 | interaction.editReply({ |
| 394 | embeds: [ |
| 395 | new EmojiEmbed() |
| 396 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 397 | .setTitle("Purge") |
| 398 | .setDescription("Messages cleared") |
| 399 | .setStatus("Success") |
| 400 | ], |
| 401 | components: [] |
| 402 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 403 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 404 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 405 | await interaction.editReply({ |
| 406 | embeds: [ |
| 407 | new EmojiEmbed() |
| 408 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 409 | .setTitle("Purge") |
| 410 | .setDescription("No changes were made") |
| 411 | .setStatus("Success") |
| 412 | ], |
| 413 | components: [] |
| 414 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 417 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 418 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 419 | const check = (interaction: CommandInteraction) => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 420 | if (!interaction.guild) return false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 421 | const member = interaction.member as GuildMember; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 422 | const me = interaction.guild.members.me!; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 423 | // Check if nucleus has the manage_messages permission |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 424 | if (!me.permissions.has("ManageMessages")) throw new Error("I do not have the *Manage Messages* permission"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 425 | // Allow the owner to purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 426 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 427 | // Check if the user has manage_messages permission |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame^] | 428 | if (!member.permissions.has("ManageMessages")) throw new Error("You do not have the *Manage Messages* permission"); |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 429 | // Allow purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 430 | return true; |
| 431 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 432 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 433 | export { command, callback, check }; |