Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 2 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 3 | import { LoadingEmbed } from "../../utils/defaults.js"; |
| 4 | import Discord, { |
| 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
| 7 | ButtonStyle, |
| 8 | ContextMenuCommandBuilder, |
| 9 | GuildMember, |
| 10 | GuildTextBasedChannel, |
| 11 | Message, |
TheCodedProf | 4b17af3 | 2023-04-23 16:18:22 -0400 | [diff] [blame] | 12 | MessageContextMenuCommandInteraction, |
| 13 | PermissionFlagsBits |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 14 | } from "discord.js"; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 15 | import client from "../../utils/client.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 16 | import { messageException } from "../../utils/createTemporaryStorage.js"; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 17 | |
Skyler Grey | f25451c | 2023-04-23 21:26:11 +0000 | [diff] [blame] | 18 | const command = new ContextMenuCommandBuilder() |
| 19 | .setName("Purge up to Here") |
| 20 | .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 21 | |
| 22 | async function waitForButton(m: Discord.Message, member: Discord.GuildMember): Promise<boolean> { |
| 23 | let component; |
| 24 | try { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 25 | component = await m.awaitMessageComponent({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 26 | time: 200000, |
| 27 | filter: (i) => i.user.id === member.id && i.channel!.id === m.channel.id && i.message.id === m.id |
| 28 | }); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 29 | } catch (e) { |
| 30 | return false; |
| 31 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 32 | await component.deferUpdate(); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 33 | return true; |
| 34 | } |
| 35 | |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 36 | const callback = async (interaction: MessageContextMenuCommandInteraction) => { |
| 37 | await interaction.targetMessage.fetch(); |
| 38 | const targetMessage = interaction.targetMessage; |
| 39 | const targetMember: Discord.User = targetMessage.author; |
| 40 | let allowedMessage: Discord.Message | undefined = undefined; |
| 41 | const channel = interaction.channel; |
| 42 | if (!channel) return; |
| 43 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 44 | const history: Discord.Collection<string, Discord.Message> = await channel.messages.fetch({ limit: 100 }); |
| 45 | if (Date.now() - targetMessage.createdTimestamp > 2 * 7 * 24 * 60 * 60 * 1000) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 46 | const m = await interaction.editReply({ |
| 47 | embeds: [ |
| 48 | new EmojiEmbed() |
| 49 | .setTitle("Purge") |
| 50 | .setDescription( |
| 51 | "The message you selected is older than 2 weeks. Discord only allows bots to delete messages that are 2 weeks old or younger." |
| 52 | ) |
| 53 | .setEmoji("CHANNEL.PURGE.RED") |
| 54 | .setStatus("Danger") |
| 55 | ], |
| 56 | components: [ |
| 57 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 58 | new ButtonBuilder() |
| 59 | .setCustomId("oldest") |
| 60 | .setLabel("Select first allowed message") |
| 61 | .setStyle(ButtonStyle.Primary) |
| 62 | ) |
| 63 | ] |
| 64 | }); |
| 65 | if (!(await waitForButton(m, interaction.member as Discord.GuildMember))) return; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 66 | } else if (!history.has(targetMessage.id)) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 67 | const m = await interaction.editReply({ |
| 68 | embeds: [ |
| 69 | new EmojiEmbed() |
| 70 | .setTitle("Purge") |
| 71 | .setDescription( |
| 72 | "The message you selected is not in the last 100 messages in this channel. Discord only allows bots to delete 100 messages at a time." |
| 73 | ) |
| 74 | .setEmoji("CHANNEL.PURGE.YELLOW") |
| 75 | .setStatus("Warning") |
| 76 | ], |
| 77 | components: [ |
| 78 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 79 | new ButtonBuilder() |
| 80 | .setCustomId("oldest") |
| 81 | .setLabel("Select first allowed message") |
| 82 | .setStyle(ButtonStyle.Primary) |
| 83 | ) |
| 84 | ] |
| 85 | }); |
| 86 | if (!(await waitForButton(m, interaction.member as Discord.GuildMember))) return; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 87 | } else { |
| 88 | allowedMessage = targetMessage; |
| 89 | } |
| 90 | |
| 91 | if (!allowedMessage) { |
| 92 | // Find the oldest message thats younger than 2 weeks |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 93 | const messages = history.filter((m) => Date.now() - m.createdTimestamp < 2 * 7 * 24 * 60 * 60 * 1000); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 94 | allowedMessage = messages.sort((a, b) => a.createdTimestamp - b.createdTimestamp).first(); |
| 95 | } |
| 96 | |
| 97 | if (!allowedMessage) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 98 | await interaction.editReply({ |
| 99 | embeds: [ |
| 100 | new EmojiEmbed() |
| 101 | .setTitle("Purge") |
| 102 | .setDescription( |
| 103 | "There are no valid messages in the last 100 messages. (No messages younger than 2 weeks)" |
| 104 | ) |
| 105 | .setEmoji("CHANNEL.PURGE.RED") |
| 106 | .setStatus("Danger") |
| 107 | ], |
| 108 | components: [] |
| 109 | }); |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 113 | let reason: string | null = null; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 114 | let confirmation; |
| 115 | let chosen = false; |
| 116 | let timedOut = false; |
| 117 | let deleteSelected = true; |
| 118 | let deleteUser = false; |
| 119 | do { |
| 120 | confirmation = await new confirmationMessage(interaction) |
| 121 | .setEmoji("CHANNEL.PURGE.RED") |
| 122 | .setTitle("Purge") |
| 123 | .setDescription( |
| 124 | `[[Selected Message]](${allowedMessage.url})\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 125 | (reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*") + |
| 126 | "\n\n" + |
| 127 | `Are you sure you want to delete all messages from below the selected message?` |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 128 | ) |
| 129 | .addCustomBoolean( |
| 130 | "includeSelected", |
| 131 | "Include selected message", |
| 132 | false, |
| 133 | undefined, |
| 134 | "The selected message will be deleted as well.", |
| 135 | "The selected message will not be deleted.", |
| 136 | "CONTROL." + (deleteSelected ? "TICK" : "CROSS"), |
| 137 | deleteSelected |
| 138 | ) |
| 139 | .addCustomBoolean( |
| 140 | "onlySelectedUser", |
| 141 | "Only selected user", |
| 142 | false, |
| 143 | undefined, |
| 144 | `Only messages from <@${targetMember.id}> will be deleted.`, |
| 145 | `All messages will be deleted.`, |
| 146 | "CONTROL." + (deleteUser ? "TICK" : "CROSS"), |
| 147 | deleteUser |
| 148 | ) |
| 149 | .setColor("Danger") |
| 150 | .addReasonButton(reason ?? "") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 151 | .setFailedMessage("No changes were made", "Success", "CHANNEL.PURGE.GREEN") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 152 | .send(true); |
| 153 | reason = reason ?? ""; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 154 | if (confirmation.cancelled) timedOut = true; |
| 155 | else if (confirmation.success !== undefined) chosen = true; |
| 156 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 157 | else if (confirmation.components) { |
| 158 | deleteSelected = confirmation.components["includeSelected"]!.active; |
| 159 | deleteUser = confirmation.components["onlySelectedUser"]!.active; |
| 160 | } |
| 161 | } while (!chosen && !timedOut); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 162 | if (timedOut || !confirmation.success) return; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 163 | const filteredMessages = history |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 164 | .filter((m) => m.createdTimestamp >= allowedMessage!.createdTimestamp) // older than selected |
| 165 | .filter((m) => (deleteUser ? m.author.id === targetMember.id : true)) // only selected user |
| 166 | .filter((m) => (deleteSelected ? true : m.id !== allowedMessage!.id)); // include selected |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 167 | |
| 168 | const deleted = await (channel as GuildTextBasedChannel).bulkDelete(filteredMessages, true); |
| 169 | if (deleted.size === 0) { |
| 170 | return await interaction.editReply({ |
| 171 | embeds: [ |
| 172 | new EmojiEmbed() |
| 173 | .setEmoji("CHANNEL.PURGE.RED") |
| 174 | .setTitle("Purge") |
| 175 | .setDescription("No messages were deleted") |
| 176 | .setStatus("Danger") |
| 177 | ], |
| 178 | components: [] |
| 179 | }); |
| 180 | } |
| 181 | if (deleteUser) { |
| 182 | await client.database.history.create( |
| 183 | "purge", |
| 184 | interaction.guild!.id, |
| 185 | targetMember, |
| 186 | interaction.user, |
| 187 | reason === "" ? "*No reason provided*" : reason, |
| 188 | null, |
| 189 | null, |
| 190 | deleted.size.toString() |
| 191 | ); |
| 192 | } |
| 193 | const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger; |
| 194 | const data = { |
| 195 | meta: { |
| 196 | type: "channelPurge", |
| 197 | displayName: "Channel Purged", |
| 198 | calculateType: "messageDelete", |
| 199 | color: NucleusColors.red, |
| 200 | emoji: "PUNISH.BAN.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 201 | timestamp: Date.now() |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 202 | }, |
| 203 | list: { |
| 204 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 205 | purgedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 206 | channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as Discord.GuildChannel)), |
| 207 | messagesCleared: entry(deleted.size.toString(), deleted.size.toString()) |
| 208 | }, |
| 209 | hidden: { |
| 210 | guild: interaction.guild!.id |
| 211 | } |
| 212 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 213 | await log(data); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 214 | const messages: Message[] = deleted |
| 215 | .map((m) => m) |
| 216 | .filter((m) => m instanceof Message) |
| 217 | .map((m) => m as Message); |
| 218 | if (messages.length === 1) messageException(interaction.guild!.id, interaction.channel.id, messages[0]!.id); |
| 219 | const messageArray: Message[] = messages |
| 220 | .filter( |
| 221 | (message) => |
| 222 | !message!.components.some((component) => |
| 223 | component.components.some((child) => child.customId?.includes("transcript") ?? false) |
| 224 | ) |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 225 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 226 | .map((message) => message as Message); |
| 227 | const transcript = await client.database.transcripts.createTranscript( |
Skyler Grey | e0c511b | 2023-03-06 10:30:17 +0000 | [diff] [blame] | 228 | "purge", |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 229 | messageArray, |
| 230 | interaction, |
| 231 | interaction.member as GuildMember |
| 232 | ); |
Samuel Shuert | cc63dee | 2023-03-03 18:54:29 -0500 | [diff] [blame] | 233 | |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 234 | const [code, key, iv] = await client.database.transcripts.create(transcript); |
Samuel Shuert | cc63dee | 2023-03-03 18:54:29 -0500 | [diff] [blame] | 235 | |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 236 | await interaction.editReply({ |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 237 | embeds: [ |
| 238 | new EmojiEmbed() |
| 239 | .setEmoji("CHANNEL.PURGE.GREEN") |
| 240 | .setTitle("Purge") |
| 241 | .setDescription("Messages cleared") |
| 242 | .setStatus("Success") |
| 243 | ], |
| 244 | components: [ |
| 245 | new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 246 | new ButtonBuilder() |
| 247 | .setLabel("View") |
| 248 | .setStyle(ButtonStyle.Link) |
| 249 | .setURL(`https://clicks.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`) |
| 250 | .setDisabled(!code) |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 251 | ]) |
| 252 | ] |
pineafan | de618c4 | 2023-02-28 18:03:18 +0000 | [diff] [blame] | 253 | }); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 254 | }; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 255 | |
| 256 | const check = async (_interaction: MessageContextMenuCommandInteraction) => { |
| 257 | return true; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 258 | }; |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 259 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 260 | export { command, callback, check }; |