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, |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 9 | ButtonStyle, |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 10 | User, |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 11 | ThreadChannel |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 12 | } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 13 | import EmojiEmbed from "../utils/generateEmojiEmbed.js"; |
| 14 | import getEmojiByName from "../utils/getEmojiByName.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 15 | import client from "../utils/client.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 16 | import { messageException } from "../utils/createTemporaryStorage.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 17 | |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 18 | const noTopic = new EmojiEmbed() |
| 19 | .setTitle("User not found") |
| 20 | .setDescription("There is no user associated with this ticket.") |
| 21 | .setStatus("Danger") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 22 | .setEmoji("CONTROL.BLOCKCROSS"); |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 23 | |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 24 | export default async function (interaction: CommandInteraction | MessageComponentInteraction) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 25 | if (interaction.channel === null) return; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 26 | if (!(interaction.channel instanceof TextChannel || interaction.channel instanceof ThreadChannel)) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 27 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 28 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 29 | let messages: Message[] = []; |
| 30 | let deletedCount: number; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 31 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 32 | do { |
| 33 | const fetched = await (interaction.channel as TextChannel).messages.fetch({ limit: 100 }); |
| 34 | const deleted = await (interaction.channel as TextChannel).bulkDelete(fetched, true); |
| 35 | deletedCount = deleted.size; |
TheCodedProf | bc94a5c | 2023-01-18 18:59:31 -0500 | [diff] [blame] | 36 | messages = messages.concat(Array.from(deleted.values() as Iterable<Message>)); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 37 | if (messages.length === 1) messageException(interaction.guild!.id, interaction.channel.id, messages[0]!.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 38 | } while (deletedCount === 100); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 39 | messages = messages.filter( |
| 40 | (message) => |
| 41 | !message.components.some((component) => |
| 42 | component.components.some((child) => child.customId?.includes("transcript") ?? false) |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 43 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 44 | ); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 45 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 46 | let topic; |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 47 | let member: GuildMember = interaction.guild?.members.me!; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 48 | if (interaction.channel instanceof TextChannel) { |
| 49 | topic = interaction.channel.topic; |
| 50 | if (topic === null) return await interaction.reply({ embeds: [noTopic] }); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 51 | const mem = interaction.guild!.members.cache.get(topic.split(" ")[1]!); |
| 52 | if (mem) member = mem; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 53 | } else { |
| 54 | topic = interaction.channel.name; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 55 | const split = topic.split("-").map((p) => p.trim()) as [string, string, string]; |
| 56 | const mem = interaction.guild!.members.cache.get(split[1]); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 57 | if (mem) member = mem; |
TheCodedProf | cfe8e9a | 2023-02-26 17:28:09 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Skyler Grey | e0c511b | 2023-03-06 10:30:17 +0000 | [diff] [blame] | 60 | const newOut = await client.database.transcripts.createTranscript("ticket", messages, interaction, member); |
TheCodedProf | 2e54a77 | 2023-02-14 16:26:47 -0500 | [diff] [blame] | 61 | |
TheCodedProf | 75c51be | 2023-03-03 17:18:18 -0500 | [diff] [blame] | 62 | const [code, key, iv] = await client.database.transcripts.create(newOut); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 63 | if (!code) |
| 64 | return await interaction.reply({ |
| 65 | embeds: [ |
| 66 | new EmojiEmbed() |
| 67 | .setTitle("Error") |
| 68 | .setDescription("An error occurred while creating the transcript.") |
| 69 | .setStatus("Danger") |
| 70 | .setEmoji("CONTROL.BLOCKCROSS") |
| 71 | ] |
| 72 | }); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 73 | const guildConfig = await client.database.guilds.read(interaction.guild!.id); |
| 74 | const m: Message = (await interaction.reply({ |
| 75 | embeds: [ |
| 76 | new EmojiEmbed() |
| 77 | .setTitle("Transcript") |
| 78 | .setDescription( |
| 79 | "You can view the transcript using the link below. You can save the link for later" + |
| 80 | (guildConfig.logging.logs.channel |
| 81 | ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.` |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 82 | : ". It will be automatically deleted after 30 days if a logging channel is not set.") |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 83 | ) |
| 84 | .setStatus("Success") |
| 85 | .setEmoji("CONTROL.DOWNLOAD") |
| 86 | ], |
| 87 | components: [ |
| 88 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 89 | new ButtonBuilder() |
| 90 | .setLabel("View") |
| 91 | .setStyle(ButtonStyle.Link) |
| 92 | .setURL(`https://testing.coded.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`), |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 93 | new ButtonBuilder() |
| 94 | .setLabel("Delete") |
| 95 | .setStyle(ButtonStyle.Danger) |
| 96 | .setCustomId("close") |
| 97 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 98 | ]) |
| 99 | ], |
| 100 | fetchReply: true |
| 101 | })) as Message; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 102 | let i; |
| 103 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 104 | i = await m.awaitMessageComponent({ |
| 105 | time: 300000, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 106 | filter: (i) => { |
| 107 | return ( |
| 108 | i.user.id === interaction.user.id && |
| 109 | i.channel!.id === interaction.channel!.id && |
| 110 | i.message.id === m.id |
| 111 | ); |
| 112 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 113 | }); |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 114 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 115 | } catch { |
| 116 | return; |
| 117 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 119 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | type: "ticketDeleted", |
| 121 | displayName: "Ticket Deleted", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 122 | calculateType: "ticketUpdate", |
| 123 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | emoji: "GUILD.TICKET.CLOSE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 125 | timestamp: Date.now() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 126 | }, |
| 127 | list: { |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 128 | ticketFor: entry(member.id, renderUser(member.user)), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 129 | deletedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as User)), |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 130 | deleted: entry(Date.now().toString(), renderDelta(Date.now())), |
| 131 | transcript: entry(code, `https://clicks.codes/nucleus/transcript/${code}`) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 132 | }, |
| 133 | hidden: { |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 134 | guild: interaction.guild!.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 135 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 137 | await log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | await interaction.channel.delete(); |
| 139 | return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 140 | } |