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