Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 1 | import { |
| 2 | CommandInteraction, |
| 3 | GuildMember, |
| 4 | Message, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 7 | MessageComponentInteraction, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 8 | TextChannel, |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 9 | ButtonStyle, |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 10 | User, |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 11 | ComponentType |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 12 | } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 13 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 14 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 15 | import { PasteClient, Publicity, ExpireDate } from "pastebin-api"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 16 | import client from "../utils/client.js"; |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame^] | 17 | import { messageException } from '../utils/createTemporaryStorage.js'; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 18 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 19 | const pbClient = new PasteClient(client.config.pastebinApiKey); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 20 | |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 21 | interface TranscriptEmbed { |
| 22 | title?: string; |
| 23 | description?: string; |
| 24 | fields?: { |
| 25 | name: string; |
| 26 | value: string; |
| 27 | inline: boolean; |
| 28 | }[]; |
| 29 | footer?: { |
| 30 | text: string; |
| 31 | iconURL?: string; |
| 32 | }; |
| 33 | } |
| 34 | |
| 35 | interface TranscriptComponent { |
| 36 | type: number; |
| 37 | style?: ButtonStyle; |
| 38 | label?: string; |
| 39 | description?: string; |
| 40 | placeholder?: string; |
| 41 | emojiURL?: string; |
| 42 | } |
| 43 | |
| 44 | interface TranscriptAuthor { |
| 45 | username: string; |
| 46 | discriminator: number; |
| 47 | nickname?: string; |
| 48 | id: string; |
| 49 | iconURL?: string; |
| 50 | topRole: { |
| 51 | color: number; |
| 52 | badgeURL?: string; |
| 53 | } |
| 54 | } |
| 55 | |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 56 | interface TranscriptAttachment { |
| 57 | url: string; |
| 58 | filename: string; |
| 59 | size: number; |
| 60 | log?: string; |
| 61 | } |
| 62 | |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 63 | interface TranscriptMessage { |
| 64 | id: string; |
| 65 | author: TranscriptAuthor; |
| 66 | content?: string; |
| 67 | embeds?: TranscriptEmbed[]; |
| 68 | components?: TranscriptComponent[][]; |
| 69 | editedTimestamp?: number; |
| 70 | createdTimestamp: number; |
| 71 | flags?: string[]; |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 72 | attachments?: TranscriptAttachment[]; |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 73 | stickerURLs?: string[]; |
| 74 | referencedMessage?: string | [string, string, string]; |
| 75 | } |
| 76 | |
| 77 | interface Transcript { |
| 78 | type: "ticket" | "purge" |
| 79 | guild: string; |
| 80 | channel: string; |
| 81 | messages: TranscriptMessage[]; |
| 82 | createdTimestamp: number; |
| 83 | createdBy: TranscriptAuthor; |
| 84 | } |
| 85 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 86 | export default async function (interaction: CommandInteraction | MessageComponentInteraction) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 87 | if (interaction.channel === null) return; |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 88 | if (!(interaction.channel instanceof TextChannel)) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 89 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 90 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 91 | let messages: Message[] = []; |
| 92 | let deletedCount: number; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 93 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 94 | do { |
| 95 | const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 }); |
| 96 | const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true); |
| 97 | deletedCount = deleted.size; |
TheCodedProf | bc94a5c | 2023-01-18 18:59:31 -0500 | [diff] [blame] | 98 | messages = messages.concat(Array.from(deleted.values() as Iterable<Message>)); |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame^] | 99 | if (messages.length === 1) messageException(interaction.guild!.id, interaction.channel.id, messages[0]!.id) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 100 | } while (deletedCount === 100); |
| 101 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | let out = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | messages.reverse().forEach((message) => { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 104 | if (!message.author.bot) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 105 | const sentDate = new Date(message.createdTimestamp); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 106 | out += `${message.author.username}#${message.author.discriminator} (${ |
| 107 | message.author.id |
| 108 | }) [${sentDate.toUTCString()}]\n`; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | const lines = message.content.split("\n"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | lines.forEach((line) => { |
| 111 | out += `> ${line}\n`; |
| 112 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 113 | out += "\n\n"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 114 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 115 | }); |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 116 | |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 117 | const interactionMember = await interaction.guild?.members.fetch(interaction.user.id) |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 118 | |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 119 | const newOut: Transcript = { |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 120 | type: "ticket", |
| 121 | guild: interaction.guild!.id, |
| 122 | channel: interaction.channel!.id, |
| 123 | messages: [], |
| 124 | createdTimestamp: Date.now(), |
| 125 | createdBy: { |
| 126 | username: interaction.user.username, |
| 127 | discriminator: parseInt(interaction.user.discriminator), |
| 128 | id: interaction.user.id, |
| 129 | topRole: { |
| 130 | color: interactionMember?.roles.highest.color ?? 0x000000 |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | if(interactionMember?.roles.icon) newOut.createdBy.topRole.badgeURL = interactionMember.roles.icon.iconURL()!; |
| 135 | messages.reverse().forEach((message) => { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 136 | const msg: TranscriptMessage = { |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 137 | id: message.id, |
| 138 | author: { |
| 139 | username: message.author.username, |
| 140 | discriminator: parseInt(message.author.discriminator), |
| 141 | id: message.author.id, |
| 142 | topRole: { |
| 143 | color: message.member!.roles.highest.color |
| 144 | } |
| 145 | }, |
| 146 | createdTimestamp: message.createdTimestamp |
| 147 | }; |
| 148 | if (message.member!.roles.icon) msg.author.topRole.badgeURL = message.member!.roles.icon.iconURL()!; |
| 149 | if (message.content) msg.content = message.content; |
| 150 | if (message.embeds.length > 0) msg.embeds = message.embeds.map(embed => { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 151 | const obj: TranscriptEmbed = {}; |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 152 | if (embed.title) obj.title = embed.title; |
| 153 | if (embed.description) obj.description = embed.description; |
| 154 | if (embed.fields.length > 0) obj.fields = embed.fields.map(field => { |
| 155 | return { |
| 156 | name: field.name, |
| 157 | value: field.value, |
| 158 | inline: field.inline ?? false |
| 159 | } |
| 160 | }); |
| 161 | if (embed.footer) obj.footer = { |
| 162 | text: embed.footer.text, |
| 163 | }; |
| 164 | if (embed.footer?.iconURL) obj.footer!.iconURL = embed.footer.iconURL; |
| 165 | return obj; |
| 166 | }); |
| 167 | if (message.components.length > 0) msg.components = message.components.map(component => component.components.map(child => { |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 168 | const obj: TranscriptComponent = { |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 169 | type: child.type |
| 170 | } |
| 171 | if (child.type === ComponentType.Button) { |
| 172 | obj.style = child.style; |
| 173 | obj.label = child.label ?? ""; |
TheCodedProf | 362c51d | 2023-02-14 16:31:33 -0500 | [diff] [blame] | 174 | } else if (child.type > 2) { |
| 175 | obj.placeholder = child.placeholder ?? ""; |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 176 | } |
| 177 | return obj |
| 178 | })); |
| 179 | if (message.editedTimestamp) msg.editedTimestamp = message.editedTimestamp; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 180 | msg.flags = message.flags.toArray(); |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 181 | |
| 182 | if (message.stickers.size > 0) msg.stickerURLs = message.stickers.map(sticker => sticker.url); |
| 183 | if (message.reference) msg.referencedMessage = [message.reference.guildId ?? "", message.reference.channelId, message.reference.messageId ?? ""]; |
| 184 | |
| 185 | }); |
| 186 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 187 | const topic = interaction.channel.topic; |
| 188 | let member: GuildMember | null = null; |
| 189 | if (topic !== null) { |
| 190 | const part = topic.split(" ")[0] ?? null; |
| 191 | if (part !== null) member = interaction.guild!.members.cache.get(part) ?? null; |
| 192 | } |
| 193 | let m: Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 194 | if (out !== "") { |
| 195 | const url = await pbClient.createPaste({ |
| 196 | code: out, |
| 197 | expireDate: ExpireDate.Never, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 198 | name: |
| 199 | `Ticket Transcript ${ |
| 200 | member ? "for " + member.user.username + "#" + member.user.discriminator + " " : "" |
| 201 | }` + `(Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 202 | publicity: Publicity.Unlisted |
| 203 | }); |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 204 | const guildConfig = await client.database.guilds.read(interaction.guild!.id); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 205 | m = (await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 206 | embeds: [ |
| 207 | new EmojiEmbed() |
| 208 | .setTitle("Transcript") |
| 209 | .setDescription( |
| 210 | "You can view the transcript using the link below. You can save the link for later" + |
| 211 | (guildConfig.logging.logs.channel |
| 212 | ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.` |
| 213 | : ".") |
| 214 | ) |
| 215 | .setStatus("Success") |
| 216 | .setEmoji("CONTROL.DOWNLOAD") |
| 217 | ], |
| 218 | components: [ |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 219 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 220 | new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(url), |
| 221 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 222 | .setLabel("Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 223 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 224 | .setCustomId("close") |
| 225 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 226 | ]) |
| 227 | ], |
| 228 | fetchReply: true |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 229 | })) as Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 230 | } else { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 231 | m = (await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 232 | embeds: [ |
| 233 | new EmojiEmbed() |
| 234 | .setTitle("Transcript") |
| 235 | .setDescription( |
| 236 | "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below." |
| 237 | ) |
| 238 | .setStatus("Success") |
| 239 | .setEmoji("CONTROL.DOWNLOAD") |
| 240 | ], |
| 241 | components: [ |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 242 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 243 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 244 | .setLabel("Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 245 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 246 | .setCustomId("close") |
| 247 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 248 | ]) |
| 249 | ], |
| 250 | fetchReply: true |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 251 | })) as Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 252 | } |
| 253 | let i; |
| 254 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 255 | i = await m.awaitMessageComponent({ |
| 256 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 257 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 258 | }); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 259 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 260 | } catch { |
| 261 | return; |
| 262 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 263 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 264 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 265 | type: "ticketDeleted", |
| 266 | displayName: "Ticket Deleted", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 267 | calculateType: "ticketUpdate", |
| 268 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 269 | emoji: "GUILD.TICKET.CLOSE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 270 | timestamp: Date.now() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 271 | }, |
| 272 | list: { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 273 | ticketFor: member ? entry(member.id, renderUser(member.user)) : entry(null, "*Unknown*"), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 274 | deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as User)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 275 | deleted: entry(Date.now().toString(), renderDelta(Date.now())) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 276 | }, |
| 277 | hidden: { |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 278 | guild: interaction.guild!.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 279 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 280 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 281 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 282 | await interaction.channel.delete(); |
| 283 | return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 284 | } |