Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 1 | import Discord, { CommandInteraction, GuildChannel, GuildMember, TextChannel, ButtonStyle, ButtonBuilder, Message } from "discord.js"; |
| 2 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
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; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [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({ |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 96 | filter: (i) => 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] | 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, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 149 | emoji: "CHANNEL.PURGE.RED", |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 150 | timestamp: Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 163 | const newOut = await client.database.transcripts.createTranscript(deleted, interaction, interaction.member as GuildMember); |
| 164 | const transcript = client.database.transcripts.toHumanReadable(newOut); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | const attachmentObject = { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 166 | attachment: Buffer.from(transcript), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 167 | name: `purge-${channel.id}-${Date.now()}.txt`, |
| 168 | description: "Purge log" |
| 169 | }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 170 | const m = (await interaction.editReply({ |
| 171 | embeds: [ |
| 172 | new EmojiEmbed() |
| 173 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 174 | .setTitle("Purge") |
| 175 | .setDescription("Messages cleared") |
| 176 | .setStatus("Success") |
| 177 | ], |
| 178 | components: [ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 179 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 180 | new Discord.ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 181 | .setCustomId("download") |
| 182 | .setLabel("Download transcript") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 183 | .setStyle(ButtonStyle.Success) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 184 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 185 | ]) |
| 186 | ] |
| 187 | })) as Discord.Message; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 188 | let component; |
| 189 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 190 | component = await m.awaitMessageComponent({ |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 191 | filter: (i) => 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] | 192 | time: 300000 |
| 193 | }); |
| 194 | } catch { |
| 195 | return; |
| 196 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 197 | if (component.customId === "download") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 198 | interaction.editReply({ |
| 199 | embeds: [ |
| 200 | new EmojiEmbed() |
| 201 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 202 | .setTitle("Purge") |
| 203 | .setDescription("Uploaded") |
| 204 | .setStatus("Success") |
| 205 | ], |
| 206 | components: [], |
| 207 | files: [attachmentObject] |
| 208 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 209 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 210 | interaction.editReply({ |
| 211 | embeds: [ |
| 212 | new EmojiEmbed() |
| 213 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 214 | .setTitle("Purge") |
| 215 | .setDescription("Messages cleared") |
| 216 | .setStatus("Success") |
| 217 | ], |
| 218 | components: [] |
| 219 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 220 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 221 | return; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 222 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 223 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 224 | .setEmoji("CHANNEL.PURGE.RED") |
| 225 | .setTitle("Purge") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 226 | .setDescription( |
| 227 | keyValueList({ |
| 228 | channel: `<#${channel.id}>`, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 229 | amount: (interaction.options.get("amount")?.value as number).toString(), |
| 230 | 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] | 231 | }) |
| 232 | ) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 233 | .setColor("Danger") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 234 | .setFailedMessage("No changes were made", "Success", "CHANNEL.PURGE.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 235 | .send(); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 236 | if (confirmation.cancelled || !confirmation.success) return; |
| 237 | let messages; |
| 238 | try { |
| 239 | if (!user) { |
| 240 | const toDelete = await (interaction.channel as TextChannel).messages.fetch({ |
| 241 | limit: interaction.options.get("amount")?.value as number |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 242 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 243 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
| 244 | } else { |
| 245 | const toDelete = ( |
| 246 | await ( |
| 247 | await (interaction.channel as TextChannel).messages.fetch({ |
| 248 | limit: 100 |
| 249 | }) |
| 250 | ).filter((m) => m.author.id === user.id) |
| 251 | ).first(interaction.options.get("amount")?.value as number); |
| 252 | messages = await (channel as TextChannel).bulkDelete(toDelete, true); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 253 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 254 | } catch (e) { |
| 255 | await interaction.editReply({ |
| 256 | embeds: [ |
| 257 | new EmojiEmbed() |
| 258 | .setEmoji("CHANNEL.PURGE.RED") |
| 259 | .setTitle("Purge") |
| 260 | .setDescription("Something went wrong and no messages were deleted") |
| 261 | .setStatus("Danger") |
| 262 | ], |
| 263 | components: [] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 264 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 265 | } |
| 266 | if (!messages) { |
| 267 | await interaction.editReply({ |
| 268 | embeds: [ |
| 269 | new EmojiEmbed() |
| 270 | .setEmoji("CHANNEL.PURGE.RED") |
| 271 | .setTitle("Purge") |
| 272 | .setDescription("No messages could be deleted") |
| 273 | .setStatus("Danger") |
| 274 | ], |
| 275 | components: [] |
| 276 | }); |
| 277 | return; |
| 278 | } |
| 279 | if (user) { |
| 280 | await client.database.history.create( |
| 281 | "purge", |
| 282 | interaction.guild.id, |
| 283 | user.user, |
| 284 | interaction.user, |
| 285 | (interaction.options.get("reason")?.value as (string | null)) ?? "*No reason provided*", |
| 286 | null, |
| 287 | null, |
| 288 | messages.size.toString() |
| 289 | ); |
| 290 | } |
| 291 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
| 292 | const data = { |
| 293 | meta: { |
| 294 | type: "channelPurge", |
| 295 | displayName: "Channel Purged", |
| 296 | calculateType: "messageDelete", |
| 297 | color: NucleusColors.red, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 298 | emoji: "CHANNEL.PURGE.RED", |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 299 | timestamp: Date.now() |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 300 | }, |
| 301 | list: { |
| 302 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 303 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 304 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as GuildChannel)), |
| 305 | messagesCleared: entry(messages.size.toString(), messages.size.toString()) |
| 306 | }, |
| 307 | hidden: { |
| 308 | guild: interaction.guild.id |
| 309 | } |
| 310 | }; |
| 311 | log(data); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 312 | const messageArray: Message[] = messages.filter(message => !( |
| 313 | message!.components.some( |
| 314 | component => component.components.some( |
| 315 | child => child.customId?.includes("transcript") ?? false |
| 316 | ) |
| 317 | ) |
| 318 | )).map(message => message as Message); |
| 319 | const newOut = await client.database.transcripts.createTranscript(messageArray, interaction, interaction.member as GuildMember); |
| 320 | |
| 321 | const code = await client.database.transcripts.create(newOut); |
| 322 | await interaction.editReply({ |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 323 | embeds: [ |
| 324 | new EmojiEmbed() |
| 325 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 326 | .setTitle("Purge") |
| 327 | .setDescription("Messages cleared") |
| 328 | .setStatus("Success") |
| 329 | ], |
| 330 | components: [ |
| 331 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 332 | new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(`https://clicks.codes/nucleus/transcript?code=${code}`), |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 333 | ]) |
| 334 | ] |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 335 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 336 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 337 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 338 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 339 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 340 | if (!interaction.guild) return false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 341 | const member = interaction.member as GuildMember; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 342 | // Check if the user has manage_messages permission |
| 343 | if (!member.permissions.has("ManageMessages")) return "You do not have the *Manage Messages* permission"; |
| 344 | if (partial) return true; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 345 | const me = interaction.guild.members.me!; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 346 | // Check if nucleus has the manage_messages permission |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 347 | 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] | 348 | // Allow the owner to purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 349 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 350 | // Allow purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 351 | return true; |
| 352 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 353 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 354 | export { command, callback, check }; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 355 | export const metadata = { |
| 356 | longDescription: "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.", |
| 357 | premiumOnly: true, |
| 358 | } |