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"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +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) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 11 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 12 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 13 | let messages = []; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 14 | let deleted = 100; |
| 15 | |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 16 | while (deleted === 100) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 17 | let fetched; |
| 18 | await (interaction.channel as TextChannel).messages.fetch({limit: 100}).then(async (ms) => { |
| 19 | fetched = await (interaction.channel as TextChannel).bulkDelete(ms, true); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 20 | }); |
| 21 | deleted = fetched.size; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 22 | if (fetched) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 23 | messages = messages.concat(fetched.map(m => m)); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 24 | } |
| 25 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 26 | let out = ""; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 27 | messages.reverse().forEach(message => { |
| 28 | if (!message.author.bot) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 29 | const sentDate = new Date(message.createdTimestamp); |
| 30 | out += `${message.author.username}#${message.author.discriminator} (${message.author.id}) [${sentDate.toUTCString()}]\n`; |
| 31 | const lines = message.content.split("\n"); |
| 32 | lines.forEach(line => {out += `> ${line}\n`;}); |
| 33 | out += "\n\n"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 34 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 35 | }); |
| 36 | const member = interaction.channel.guild.members.cache.get(interaction.channel.topic.split(" ")[0]); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 37 | let m; |
| 38 | if (out !== "") { |
| 39 | const url = await pbClient.createPaste({ |
| 40 | code: out, |
| 41 | expireDate: ExpireDate.Never, |
| 42 | name: `Ticket Transcript for ${member.user.username}#${member.user.discriminator} (Created at ${new Date(interaction.channel.createdTimestamp).toDateString()})`, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 43 | publicity: Publicity.Unlisted |
| 44 | }); |
| 45 | const guildConfig = await client.database.guilds.read(interaction.guild.id); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 46 | m = await interaction.reply({embeds: [new EmojiEmbed() |
| 47 | .setTitle("Transcript") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 48 | .setDescription("You can view the transcript using the link below. You can save the link for later" + (guildConfig.logging.logs.channel ? |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 49 | ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.` |
| 50 | : ".")) |
| 51 | .setStatus("Success") |
| 52 | .setEmoji("CONTROL.DOWNLOAD") |
| 53 | ], components: [new MessageActionRow().addComponents([ |
| 54 | new MessageButton() |
| 55 | .setLabel("View") |
| 56 | .setStyle("LINK") |
| 57 | .setURL(url), |
| 58 | new MessageButton() |
| 59 | .setLabel("Delete") |
| 60 | .setStyle("DANGER") |
| 61 | .setCustomId("close") |
| 62 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 63 | ])], fetchReply: true}); |
| 64 | } else { |
| 65 | m = await interaction.reply({embeds: [new EmojiEmbed() |
| 66 | .setTitle("Transcript") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 67 | .setDescription("The transcript was empty, so no changes were made. To delete this ticket, press the delete button below.") |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 68 | .setStatus("Success") |
| 69 | .setEmoji("CONTROL.DOWNLOAD") |
| 70 | ], components: [new MessageActionRow().addComponents([ |
| 71 | new MessageButton() |
| 72 | .setLabel("Delete") |
| 73 | .setStyle("DANGER") |
| 74 | .setCustomId("close") |
| 75 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 76 | ])], fetchReply: true}); |
| 77 | } |
| 78 | let i; |
| 79 | try { |
| 80 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 81 | i.deferUpdate(); |
| 82 | } catch { return; } |
| 83 | const data = { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 84 | meta:{ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 85 | type: "ticketDeleted", |
| 86 | displayName: "Ticket Deleted", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 87 | calculateType: "ticketUpdate", |
| 88 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 89 | emoji: "GUILD.TICKET.CLOSE", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 90 | timestamp: new Date().getTime() |
| 91 | }, |
| 92 | list: { |
| 93 | ticketFor: entry(interaction.channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(interaction.channel.topic.split(" ")[0])).user)), |
| 94 | deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 95 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
| 96 | }, |
| 97 | hidden: { |
| 98 | guild: interaction.guild.id |
| 99 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 100 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 101 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 102 | await interaction.channel.delete(); |
| 103 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 104 | } |