Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 1 | import Discord, { |
| 2 | CommandInteraction, |
| 3 | GuildChannel, |
| 4 | GuildMember, |
| 5 | TextChannel, |
| 6 | ButtonStyle, |
| 7 | ButtonBuilder, |
| 8 | Message |
| 9 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 10 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 11 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 12 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 13 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 14 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 15 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 16 | |
| 17 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 18 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | .setName("purge") |
| 20 | .setDescription("Bulk deletes messages in a channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 21 | .addIntegerOption((option) => |
| 22 | option |
| 23 | .setName("amount") |
| 24 | .setDescription("The amount of messages to delete") |
| 25 | .setRequired(false) |
| 26 | .setMinValue(1) |
| 27 | .setMaxValue(100) |
| 28 | ) |
| 29 | .addUserOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 30 | option.setName("user").setDescription("The user to purge messages from").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 31 | ) |
| 32 | .addStringOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 33 | option.setName("reason").setDescription("The reason for the purge").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 35 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 36 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 37 | if (!interaction.guild) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 38 | const user = interaction.options.getMember("user") as GuildMember | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | const channel = interaction.channel as GuildChannel; |
TheCodedProf | 088b1b2 | 2023-02-28 17:31:11 -0500 | [diff] [blame] | 40 | if (!channel.isTextBased()) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 41 | return await interaction.reply({ |
| 42 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 43 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 44 | .setEmoji("CHANNEL.PURGE.RED") |
| 45 | .setTitle("Purge") |
| 46 | .setDescription("You cannot purge this channel") |
| 47 | .setStatus("Danger") |
| 48 | ], |
| 49 | components: [], |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | ephemeral: true |
| 51 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 52 | } |
| 53 | // TODO:[Modals] Replace this with a modal |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 54 | if (!interaction.options.get("amount")) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 55 | await interaction.reply({ |
| 56 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 57 | new EmojiEmbed() |
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") |
| 61 | .setStatus("Danger") |
| 62 | ], |
| 63 | components: [], |
| 64 | ephemeral: true, |
| 65 | fetchReply: true |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | }); |
| 67 | let deleted = [] as Discord.Message[]; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 68 | let timedOut = false; |
| 69 | let amountSelected = false; |
| 70 | while (!timedOut && !amountSelected) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | const m = (await interaction.editReply({ |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 72 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 73 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 74 | .setEmoji("CHANNEL.PURGE.RED") |
| 75 | .setTitle("Purge") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 76 | .setDescription( |
| 77 | "Select how many messages to delete. You can continue clicking until all messages are cleared." |
| 78 | ) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 79 | .setStatus("Danger") |
| 80 | ], |
| 81 | components: [ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 82 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 83 | new Discord.ButtonBuilder().setCustomId("1").setLabel("1").setStyle(ButtonStyle.Secondary), |
| 84 | new Discord.ButtonBuilder().setCustomId("3").setLabel("3").setStyle(ButtonStyle.Secondary), |
| 85 | new Discord.ButtonBuilder().setCustomId("5").setLabel("5").setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 86 | ]), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 87 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 88 | new Discord.ButtonBuilder().setCustomId("10").setLabel("10").setStyle(ButtonStyle.Secondary), |
| 89 | new Discord.ButtonBuilder().setCustomId("25").setLabel("25").setStyle(ButtonStyle.Secondary), |
| 90 | new Discord.ButtonBuilder().setCustomId("50").setLabel("50").setStyle(ButtonStyle.Secondary) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 91 | ]), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 92 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 93 | new Discord.ButtonBuilder() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 94 | .setCustomId("done") |
| 95 | .setLabel("Done") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 96 | .setStyle(ButtonStyle.Success) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 97 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 98 | ]) |
| 99 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 100 | })) as Discord.Message; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 101 | let component; |
| 102 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | component = m.awaitMessageComponent({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 104 | filter: (i) => |
| 105 | i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.id === m.id, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 106 | time: 300000 |
| 107 | }); |
| 108 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 109 | timedOut = 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 | (await component).deferUpdate(); |
| 113 | if ((await component).customId === "done") { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 114 | amountSelected = true; |
| 115 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 116 | } |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 117 | const amount = parseInt((await component).customId); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 118 | |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 119 | let messages: Discord.Message[] = []; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 120 | await (interaction.channel as TextChannel).messages.fetch({ limit: amount }).then(async (ms) => { |
| 121 | if (user) { |
| 122 | ms = ms.filter((m) => m.author.id === user.id); |
| 123 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 124 | 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] | 125 | }); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 126 | deleted = deleted.concat(messages); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 127 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | if (deleted.length === 0) |
| 129 | return await interaction.editReply({ |
| 130 | embeds: [ |
| 131 | new EmojiEmbed() |
| 132 | .setEmoji("CHANNEL.PURGE.RED") |
| 133 | .setTitle("Purge") |
| 134 | .setDescription("No messages were deleted") |
| 135 | .setStatus("Danger") |
| 136 | ], |
| 137 | components: [] |
| 138 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 139 | if (user) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 140 | await client.database.history.create( |
| 141 | "purge", |
| 142 | interaction.guild.id, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 143 | user.user, |
| 144 | interaction.user, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 145 | (interaction.options.get("reason")?.value as string | null) ?? "*No reason provided*", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | null, |
| 147 | null, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 148 | deleted.length.toString() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 150 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 151 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 152 | const data = { |
| 153 | meta: { |
| 154 | type: "channelPurge", |
| 155 | displayName: "Channel Purged", |
| 156 | calculateType: "messageDelete", |
| 157 | color: NucleusColors.red, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 158 | emoji: "CHANNEL.PURGE.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 159 | timestamp: Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 160 | }, |
| 161 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 162 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 163 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 164 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as GuildChannel)), |
| 165 | messagesCleared: entry(deleted.length.toString(), deleted.length.toString()) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 166 | }, |
| 167 | hidden: { |
| 168 | guild: interaction.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 169 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 170 | }; |
| 171 | log(data); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 172 | const newOut = await client.database.transcripts.createTranscript( |
| 173 | deleted, |
| 174 | interaction, |
| 175 | interaction.member as GuildMember |
| 176 | ); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 177 | const transcript = client.database.transcripts.toHumanReadable(newOut); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 178 | const attachmentObject = { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 179 | attachment: Buffer.from(transcript), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 204 | filter: (i) => |
| 205 | i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.id === m.id, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 206 | time: 300000 |
| 207 | }); |
| 208 | } catch { |
| 209 | return; |
| 210 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 211 | if (component.customId === "download") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 212 | interaction.editReply({ |
| 213 | embeds: [ |
| 214 | new EmojiEmbed() |
| 215 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 216 | .setTitle("Purge") |
| 217 | .setDescription("Uploaded") |
| 218 | .setStatus("Success") |
| 219 | ], |
| 220 | components: [], |
| 221 | files: [attachmentObject] |
| 222 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 223 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 224 | interaction.editReply({ |
| 225 | embeds: [ |
| 226 | new EmojiEmbed() |
| 227 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 228 | .setTitle("Purge") |
| 229 | .setDescription("Messages cleared") |
| 230 | .setStatus("Success") |
| 231 | ], |
| 232 | components: [] |
| 233 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 234 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 235 | return; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 236 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 237 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 238 | .setEmoji("CHANNEL.PURGE.RED") |
| 239 | .setTitle("Purge") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 240 | .setDescription( |
| 241 | keyValueList({ |
| 242 | channel: `<#${channel.id}>`, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 243 | amount: (interaction.options.get("amount")?.value as number).toString(), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 244 | reason: `\n> ${ |
| 245 | interaction.options.get("reason")?.value |
| 246 | ? interaction.options.get("reason")?.value |
| 247 | : "*No reason provided*" |
| 248 | }` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 249 | }) |
| 250 | ) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 251 | .setColor("Danger") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 252 | .setFailedMessage("No changes were made", "Success", "CHANNEL.PURGE.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 253 | .send(); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 254 | if (confirmation.cancelled || !confirmation.success) return; |
| 255 | let messages; |
| 256 | try { |
| 257 | if (!user) { |
| 258 | const toDelete = await (interaction.channel as TextChannel).messages.fetch({ |
| 259 | limit: interaction.options.get("amount")?.value as number |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 260 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 261 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
| 262 | } else { |
| 263 | const toDelete = ( |
| 264 | await ( |
| 265 | await (interaction.channel as TextChannel).messages.fetch({ |
| 266 | limit: 100 |
| 267 | }) |
| 268 | ).filter((m) => m.author.id === user.id) |
| 269 | ).first(interaction.options.get("amount")?.value as number); |
| 270 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 271 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 272 | } catch (e) { |
| 273 | await interaction.editReply({ |
| 274 | embeds: [ |
| 275 | new EmojiEmbed() |
| 276 | .setEmoji("CHANNEL.PURGE.RED") |
| 277 | .setTitle("Purge") |
| 278 | .setDescription("Something went wrong and no messages were deleted") |
| 279 | .setStatus("Danger") |
| 280 | ], |
| 281 | components: [] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 282 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 283 | } |
| 284 | if (!messages) { |
| 285 | await interaction.editReply({ |
| 286 | embeds: [ |
| 287 | new EmojiEmbed() |
| 288 | .setEmoji("CHANNEL.PURGE.RED") |
| 289 | .setTitle("Purge") |
| 290 | .setDescription("No messages could be deleted") |
| 291 | .setStatus("Danger") |
| 292 | ], |
| 293 | components: [] |
| 294 | }); |
| 295 | return; |
| 296 | } |
| 297 | if (user) { |
| 298 | await client.database.history.create( |
| 299 | "purge", |
| 300 | interaction.guild.id, |
| 301 | user.user, |
| 302 | interaction.user, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 303 | (interaction.options.get("reason")?.value as string | null) ?? "*No reason provided*", |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 304 | null, |
| 305 | null, |
| 306 | messages.size.toString() |
| 307 | ); |
| 308 | } |
| 309 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
| 310 | const data = { |
| 311 | meta: { |
| 312 | type: "channelPurge", |
| 313 | displayName: "Channel Purged", |
| 314 | calculateType: "messageDelete", |
| 315 | color: NucleusColors.red, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 316 | emoji: "CHANNEL.PURGE.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 317 | timestamp: Date.now() |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 318 | }, |
| 319 | list: { |
| 320 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 321 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 322 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as GuildChannel)), |
| 323 | messagesCleared: entry(messages.size.toString(), messages.size.toString()) |
| 324 | }, |
| 325 | hidden: { |
| 326 | guild: interaction.guild.id |
| 327 | } |
| 328 | }; |
| 329 | log(data); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 330 | const messageArray: Message[] = messages |
| 331 | .filter( |
| 332 | (message) => |
| 333 | !message!.components.some((component) => |
| 334 | component.components.some((child) => child.customId?.includes("transcript") ?? false) |
| 335 | ) |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 336 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 337 | .map((message) => message as Message); |
| 338 | const newOut = await client.database.transcripts.createTranscript( |
| 339 | messageArray, |
| 340 | interaction, |
| 341 | interaction.member as GuildMember |
| 342 | ); |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 343 | |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 344 | const [code, key, iv] = await client.database.transcripts.create(newOut); |
Samuel Shuert | cc63dee | 2023-03-03 18:54:29 -0500 | [diff] [blame] | 345 | |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 346 | await interaction.editReply({ |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 347 | embeds: [ |
| 348 | new EmojiEmbed() |
| 349 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 350 | .setTitle("Purge") |
| 351 | .setDescription("Messages cleared") |
| 352 | .setStatus("Success") |
| 353 | ], |
| 354 | components: [ |
| 355 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 356 | new ButtonBuilder() |
| 357 | .setLabel("View") |
| 358 | .setStyle(ButtonStyle.Link) |
| 359 | .setURL(`https://clicks.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`) |
| 360 | .setDisabled(!code) |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 361 | ]) |
| 362 | ] |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 363 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 364 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 365 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 366 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 367 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 368 | if (!interaction.guild) return false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 369 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 370 | // Check if the user has manage_messages permission |
| 371 | if (!member.permissions.has("ManageMessages")) return "You do not have the *Manage Messages* permission"; |
| 372 | if (partial) return true; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 373 | const me = interaction.guild.members.me!; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 374 | // Check if nucleus has the manage_messages permission |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 375 | if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 376 | // Allow the owner to purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 377 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 378 | // Allow purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 379 | return true; |
| 380 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 381 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 382 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 383 | export const metadata = { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 384 | longDescription: |
| 385 | "Deletes a specified amount of messages from a channel, optionally from a specific user. Without an amount, you can repeatedly choose a number of messages to delete.", |
| 386 | premiumOnly: true |
| 387 | }; |