PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { JSONTranscriptFromMessageArray, JSONTranscriptToHumanReadable } from '../../utils/logTranscripts.js'; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 2 | import Discord, { CommandInteraction, GuildChannel, GuildMember, TextChannel, ButtonStyle, ButtonBuilder } from "discord.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 3 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 4 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 5 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 7 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 8 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | .setName("purge") |
| 13 | .setDescription("Bulk deletes messages in a channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | .addIntegerOption((option) => |
| 15 | option |
| 16 | .setName("amount") |
| 17 | .setDescription("The amount of messages to delete") |
| 18 | .setRequired(false) |
| 19 | .setMinValue(1) |
| 20 | .setMaxValue(100) |
| 21 | ) |
| 22 | .addUserOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 23 | option.setName("user").setDescription("The user to purge messages from").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | ) |
| 25 | .addStringOption((option) => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 26 | option.setName("reason").setDescription("The reason for the purge").setRequired(false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 28 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 29 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 30 | if (!interaction.guild) return; |
| 31 | const user = (interaction.options.getMember("user") as GuildMember | null); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | const channel = interaction.channel as GuildChannel; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 33 | if (channel.isTextBased()) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 34 | return await interaction.reply({ |
| 35 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 36 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 37 | .setEmoji("CHANNEL.PURGE.RED") |
| 38 | .setTitle("Purge") |
| 39 | .setDescription("You cannot purge this channel") |
| 40 | .setStatus("Danger") |
| 41 | ], |
| 42 | components: [], |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | ephemeral: true |
| 44 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 45 | } |
| 46 | // TODO:[Modals] Replace this with a modal |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 47 | if (!interaction.options.get("amount")) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 48 | await interaction.reply({ |
| 49 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 50 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | .setEmoji("CHANNEL.PURGE.RED") |
| 52 | .setTitle("Purge") |
| 53 | .setDescription("Select how many messages to delete") |
| 54 | .setStatus("Danger") |
| 55 | ], |
| 56 | components: [], |
| 57 | ephemeral: true, |
| 58 | fetchReply: true |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | }); |
| 60 | let deleted = [] as Discord.Message[]; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 61 | let timedOut = false; |
| 62 | let amountSelected = false; |
| 63 | while (!timedOut && !amountSelected) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | const m = (await interaction.editReply({ |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 65 | embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 66 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 67 | .setEmoji("CHANNEL.PURGE.RED") |
| 68 | .setTitle("Purge") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | .setDescription( |
| 70 | "Select how many messages to delete. You can continue clicking until all messages are cleared." |
| 71 | ) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 72 | .setStatus("Danger") |
| 73 | ], |
| 74 | components: [ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 75 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 76 | new Discord.ButtonBuilder().setCustomId("1").setLabel("1").setStyle(ButtonStyle.Secondary), |
| 77 | new Discord.ButtonBuilder().setCustomId("3").setLabel("3").setStyle(ButtonStyle.Secondary), |
| 78 | new Discord.ButtonBuilder().setCustomId("5").setLabel("5").setStyle(ButtonStyle.Secondary) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | ]), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 80 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 81 | new Discord.ButtonBuilder().setCustomId("10").setLabel("10").setStyle(ButtonStyle.Secondary), |
| 82 | new Discord.ButtonBuilder().setCustomId("25").setLabel("25").setStyle(ButtonStyle.Secondary), |
| 83 | new Discord.ButtonBuilder().setCustomId("50").setLabel("50").setStyle(ButtonStyle.Secondary) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 84 | ]), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 85 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 86 | new Discord.ButtonBuilder() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 87 | .setCustomId("done") |
| 88 | .setLabel("Done") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 89 | .setStyle(ButtonStyle.Success) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 90 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 91 | ]) |
| 92 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | })) as Discord.Message; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 94 | let component; |
| 95 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | component = m.awaitMessageComponent({ |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 97 | 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] | 98 | time: 300000 |
| 99 | }); |
| 100 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 101 | timedOut = true; |
| 102 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | } |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 104 | (await component).deferUpdate(); |
| 105 | if ((await component).customId === "done") { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 106 | amountSelected = true; |
| 107 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 108 | } |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 109 | const amount = parseInt((await component).customId); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 110 | |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 111 | let messages: Discord.Message[] = []; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 112 | await (interaction.channel as TextChannel).messages.fetch({ limit: amount }).then(async (ms) => { |
| 113 | if (user) { |
| 114 | ms = ms.filter((m) => m.author.id === user.id); |
| 115 | } |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 116 | 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] | 117 | }); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 118 | deleted = deleted.concat(messages); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 119 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 120 | if (deleted.length === 0) |
| 121 | return await interaction.editReply({ |
| 122 | embeds: [ |
| 123 | new EmojiEmbed() |
| 124 | .setEmoji("CHANNEL.PURGE.RED") |
| 125 | .setTitle("Purge") |
| 126 | .setDescription("No messages were deleted") |
| 127 | .setStatus("Danger") |
| 128 | ], |
| 129 | components: [] |
| 130 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 131 | if (user) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | await client.database.history.create( |
| 133 | "purge", |
| 134 | interaction.guild.id, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 135 | user.user, |
| 136 | interaction.user, |
| 137 | (interaction.options.get("reason")?.value as (string | null)) ?? "*No reason provided*", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | null, |
| 139 | null, |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 140 | deleted.length.toString() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 142 | } |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 143 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | const data = { |
| 145 | meta: { |
| 146 | type: "channelPurge", |
| 147 | displayName: "Channel Purged", |
| 148 | calculateType: "messageDelete", |
| 149 | color: NucleusColors.red, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 150 | emoji: "CHANNEL.PURGE.RED", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 151 | timestamp: new Date().getTime() |
| 152 | }, |
| 153 | list: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 154 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 155 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 156 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as GuildChannel)), |
| 157 | messagesCleared: entry(deleted.length.toString(), deleted.length.toString()) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 158 | }, |
| 159 | hidden: { |
| 160 | guild: interaction.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 161 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 162 | }; |
| 163 | log(data); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 164 | const transcript = JSONTranscriptToHumanReadable(JSONTranscriptFromMessageArray(deleted)!); |
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({ |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -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", |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 299 | timestamp: new Date().getTime() |
| 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); |
| 312 | let out = ""; |
| 313 | messages.reverse().forEach((message) => { |
| 314 | if (!message) { |
| 315 | out += "Unknown message\n\n" |
| 316 | } else { |
| 317 | const author = message.author ?? { username: "Unknown", discriminator: "0000", id: "Unknown" }; |
| 318 | out += `${author.username}#${author.discriminator} (${author.id}) [${new Date( |
| 319 | message.createdTimestamp |
| 320 | ).toISOString()}]\n`; |
| 321 | if (message.content) { |
| 322 | const lines = message.content.split("\n"); |
| 323 | lines.forEach((line) => { |
| 324 | out += `> ${line}\n`; |
| 325 | }); |
| 326 | } |
| 327 | if (message.attachments.size > 0) { |
| 328 | message.attachments.forEach((attachment) => { |
| 329 | out += `Attachment > ${attachment.url}\n`; |
| 330 | }); |
| 331 | } |
| 332 | out += "\n\n"; |
| 333 | } |
| 334 | }); |
| 335 | const attachmentObject = { |
| 336 | attachment: Buffer.from(out), |
| 337 | name: `purge-${channel.id}-${Date.now()}.txt`, |
| 338 | description: "Purge log" |
| 339 | }; |
| 340 | const m = (await interaction.editReply({ |
| 341 | embeds: [ |
| 342 | new EmojiEmbed() |
| 343 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 344 | .setTitle("Purge") |
| 345 | .setDescription("Messages cleared") |
| 346 | .setStatus("Success") |
| 347 | ], |
| 348 | components: [ |
| 349 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
| 350 | new Discord.ButtonBuilder() |
| 351 | .setCustomId("download") |
| 352 | .setLabel("Download transcript") |
| 353 | .setStyle(ButtonStyle.Success) |
| 354 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 355 | ]) |
| 356 | ] |
| 357 | })) as Discord.Message; |
| 358 | let component; |
| 359 | try { |
| 360 | component = await m.awaitMessageComponent({ |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 361 | filter: (i) => i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.id === m.id, |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 362 | time: 300000 |
| 363 | }); |
| 364 | } catch { |
| 365 | return; |
| 366 | } |
| 367 | if (component.customId === "download") { |
| 368 | interaction.editReply({ |
| 369 | embeds: [ |
| 370 | new EmojiEmbed() |
| 371 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 372 | .setTitle("Purge") |
| 373 | .setDescription("Transcript uploaded above") |
| 374 | .setStatus("Success") |
| 375 | ], |
| 376 | components: [], |
| 377 | files: [attachmentObject] |
| 378 | }); |
| 379 | } else { |
| 380 | interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 381 | embeds: [ |
| 382 | new EmojiEmbed() |
| 383 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 384 | .setTitle("Purge") |
| 385 | .setDescription("Messages cleared") |
| 386 | .setStatus("Success") |
| 387 | ], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 388 | components: [] |
| 389 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 392 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 393 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 394 | const check = (interaction: CommandInteraction) => { |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 395 | if (!interaction.guild) return false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 396 | const member = interaction.member as GuildMember; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 397 | const me = interaction.guild.members.me!; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 398 | // Check if nucleus has the manage_messages permission |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 399 | 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] | 400 | // Allow the owner to purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 401 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 402 | // Check if the user has manage_messages permission |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 403 | if (!member.permissions.has("ManageMessages")) return "You do not have the *Manage Messages* permission"; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 404 | // Allow purge |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 405 | return true; |
| 406 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 407 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 408 | export { command, callback, check }; |