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