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