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