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