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