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