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, |
| 10 | User |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 11 | } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 12 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 13 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 14 | import { PasteClient, Publicity, ExpireDate } from "pastebin-api"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 15 | import client from "../utils/client.js"; |
| 16 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 17 | const pbClient = new PasteClient(client.config.pastebinApiKey); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 18 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 19 | export default async function (interaction: CommandInteraction | MessageComponentInteraction) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 20 | if (interaction.channel === null) return; |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 21 | if (!(interaction.channel instanceof TextChannel)) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 22 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 23 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 24 | let messages: Message[] = []; |
| 25 | let deletedCount: number; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 26 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 27 | do { |
| 28 | const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 }); |
| 29 | const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true); |
| 30 | deletedCount = deleted.size; |
TheCodedProf | bc94a5c | 2023-01-18 18:59:31 -0500 | [diff] [blame] | 31 | messages = messages.concat(Array.from(deleted.values() as Iterable<Message>)); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 32 | } while (deletedCount === 100); |
| 33 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | let out = ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | messages.reverse().forEach((message) => { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 36 | if (!message.author.bot) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | const sentDate = new Date(message.createdTimestamp); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 38 | out += `${message.author.username}#${message.author.discriminator} (${ |
| 39 | message.author.id |
| 40 | }) [${sentDate.toUTCString()}]\n`; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 41 | const lines = message.content.split("\n"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 42 | lines.forEach((line) => { |
| 43 | out += `> ${line}\n`; |
| 44 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | out += "\n\n"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 46 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 47 | }); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 48 | const topic = interaction.channel.topic; |
| 49 | let member: GuildMember | null = null; |
| 50 | if (topic !== null) { |
| 51 | const part = topic.split(" ")[0] ?? null; |
| 52 | if (part !== null) member = interaction.guild!.members.cache.get(part) ?? null; |
| 53 | } |
| 54 | let m: Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 55 | if (out !== "") { |
| 56 | const url = await pbClient.createPaste({ |
| 57 | code: out, |
| 58 | expireDate: ExpireDate.Never, |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 59 | name: |
| 60 | `Ticket Transcript ${ |
| 61 | member ? "for " + member.user.username + "#" + member.user.discriminator + " " : "" |
| 62 | }` + `(Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | publicity: Publicity.Unlisted |
| 64 | }); |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 65 | const guildConfig = await client.database.guilds.read(interaction.guild!.id); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 66 | m = (await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | embeds: [ |
| 68 | new EmojiEmbed() |
| 69 | .setTitle("Transcript") |
| 70 | .setDescription( |
| 71 | "You can view the transcript using the link below. You can save the link for later" + |
| 72 | (guildConfig.logging.logs.channel |
| 73 | ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.` |
| 74 | : ".") |
| 75 | ) |
| 76 | .setStatus("Success") |
| 77 | .setEmoji("CONTROL.DOWNLOAD") |
| 78 | ], |
| 79 | components: [ |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 80 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 81 | new ButtonBuilder().setLabel("View").setStyle(ButtonStyle.Link).setURL(url), |
| 82 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | .setLabel("Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 84 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | .setCustomId("close") |
| 86 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 87 | ]) |
| 88 | ], |
| 89 | fetchReply: true |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 90 | })) as Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 91 | } else { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 92 | m = (await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | embeds: [ |
| 94 | new EmojiEmbed() |
| 95 | .setTitle("Transcript") |
| 96 | .setDescription( |
| 97 | "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below." |
| 98 | ) |
| 99 | .setStatus("Success") |
| 100 | .setEmoji("CONTROL.DOWNLOAD") |
| 101 | ], |
| 102 | components: [ |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 103 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 104 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | .setLabel("Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 106 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | .setCustomId("close") |
| 108 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 109 | ]) |
| 110 | ], |
| 111 | fetchReply: true |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 112 | })) as Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 113 | } |
| 114 | let i; |
| 115 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 116 | i = await m.awaitMessageComponent({ |
| 117 | time: 300000, |
| 118 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id } |
| 119 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | } catch { |
| 122 | return; |
| 123 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 125 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | type: "ticketDeleted", |
| 127 | displayName: "Ticket Deleted", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 128 | calculateType: "ticketUpdate", |
| 129 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 130 | emoji: "GUILD.TICKET.CLOSE", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 131 | timestamp: new Date().getTime() |
| 132 | }, |
| 133 | list: { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 134 | ticketFor: member ? entry(member.id, renderUser(member.user)) : entry(null, "*Unknown*"), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 135 | deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as User)), |
| 136 | deleted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 137 | }, |
| 138 | hidden: { |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 139 | guild: interaction.guild!.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 140 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 141 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 142 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | await interaction.channel.delete(); |
| 144 | return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 145 | } |