Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 1 | import { |
| 2 | CommandInteraction, |
| 3 | DMChannel, |
| 4 | Message, |
| 5 | MessageActionRow, |
| 6 | MessageButton, |
| 7 | PartialGroupDMChannel, |
| 8 | TextChannel |
| 9 | } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 10 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 11 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 12 | import { PasteClient, Publicity, ExpireDate } from "pastebin-api"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | import config from "../config/main.json" assert { type: "json" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 14 | import client from "../utils/client.js"; |
| 15 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | const pbClient = new PasteClient(config.pastebinApiKey); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 17 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 18 | export default async function (interaction: CommandInteraction) { |
| 19 | if (interaction.channel === null) return; |
| 20 | if (interaction.channel instanceof DMChannel) return; |
| 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 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 47 | const member = interaction.channel.guild.members.cache.get(interaction.channel.topic.split(" ")[0]); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 48 | let m; |
| 49 | if (out !== "") { |
| 50 | const url = await pbClient.createPaste({ |
| 51 | code: out, |
| 52 | expireDate: ExpireDate.Never, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 53 | name: `Ticket Transcript for ${member.user.username}#${member.user.discriminator} (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 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +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: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | ticketFor: entry( |
| 125 | interaction.channel.topic.split(" ")[0], |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 126 | renderUser((await interaction.guild.members.fetch(interaction.channel.topic.split(" ")[0])).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 127 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 128 | deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 129 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 130 | }, |
| 131 | hidden: { |
| 132 | guild: interaction.guild.id |
| 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 | } |