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