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