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