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