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