Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { getCommandMentionByName } from "../../utils/getCommandDataByName.js"; |
| 2 | import Discord, { |
| 3 | ActionRowBuilder, |
| 4 | ButtonBuilder, |
| 5 | ButtonInteraction, |
| 6 | PrivateThreadChannel, |
| 7 | TextChannel, |
| 8 | ButtonStyle, |
| 9 | CategoryChannel |
| 10 | } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 11 | import client from "../../utils/client.js"; |
| 12 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 13 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 14 | import { preloadPage } from "../../utils/createTemporaryStorage.js"; |
| 15 | import { LoadingEmbed } from "../../utils/defaults.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 16 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 17 | export default async function (interaction: Discord.CommandInteraction | ButtonInteraction) { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 18 | if (!interaction.guild) return; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 19 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 20 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 21 | const ticketChannel = config.tickets.category; |
| 22 | if (!("parent" in interaction.channel!)) { |
| 23 | return await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | embeds: [ |
| 25 | new EmojiEmbed() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 26 | .setTitle("Not a ticket") |
| 27 | .setDescription("This channel isn't a ticket, so you can't delete it.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 28 | .setStatus("Danger") |
| 29 | .setEmoji("CONTROL.BLOCKCROSS") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 30 | ], |
| 31 | ephemeral: true |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | }); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 33 | } else if (interaction.channel!.parent!.id !== ticketChannel) { |
| 34 | return await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | embeds: [ |
| 36 | new EmojiEmbed() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 37 | .setTitle("Not a ticket") |
| 38 | .setDescription("This channel isn't a ticket, so you can't delete it.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | .setStatus("Danger") |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 40 | .setEmoji("CONTROL.BLOCKCROSS") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 41 | ], |
| 42 | ephemeral: true |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 43 | }); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 44 | } |
| 45 | const channel: PrivateThreadChannel | TextChannel = interaction.channel as PrivateThreadChannel | TextChannel; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 46 | let status: string | null = "topic" in interaction.channel ? interaction.channel!.topic : interaction.channel.name; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 47 | status = status ?? ""; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | if (status.endsWith("Archived")) { |
| 49 | status = "Archived"; |
| 50 | } else { |
| 51 | status = "Active"; |
| 52 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 53 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 54 | const uID = |
| 55 | channel.type === Discord.ChannelType.PrivateThread |
| 56 | ? channel.name.split(" - ")[1] |
| 57 | : channel.topic!.split(" ")[0]; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 58 | |
| 59 | if (status === "Archived") { |
| 60 | // Delete the ticket |
| 61 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | meta: { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 64 | type: "ticketClosed", |
| 65 | displayName: "Ticket Closed", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 66 | calculateType: "ticketUpdate", |
| 67 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | emoji: "GUILD.TICKET.CLOSE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 69 | timestamp: Date.now() |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 70 | }, |
| 71 | list: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 72 | ticketFor: entry(uID!, renderUser((await interaction.guild.members.fetch(uID!)).user)), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 73 | closedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 74 | closed: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 75 | ticketChannel: entry(channel.id, channel.name) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 76 | }, |
| 77 | hidden: { |
| 78 | guild: interaction.guild.id |
| 79 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 81 | await log(data); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 82 | |
| 83 | await channel.delete(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 84 | } else if (status === "Active") { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 85 | await interaction.reply({ embeds: LoadingEmbed, fetchReply: true }); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 86 | // Archive the ticket |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 87 | await interaction.channel.fetch(); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 88 | if (channel.isThread()) { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 89 | await channel.setName(`${channel.name.replace("Active", "Archived")}`); |
| 90 | await channel.members.remove(channel.name.split(" - ")[1]!); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 91 | } else { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 92 | await channel.setTopic(`${(channel.topic ?? "").replace("Active", "Archived")}`); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 93 | if (!channel.topic!.includes("Archived")) { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 94 | await channel.setTopic("0 Archived"); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 95 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 96 | await channel.permissionOverwrites.delete(channel.topic!.split(" ")[0]!); |
| 97 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 98 | preloadPage(interaction.channel.id, "privacy", "2"); |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 99 | const hasPremium = await client.database.premium.hasPremium(interaction.guild.id); |
| 100 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | embeds: [ |
| 102 | new EmojiEmbed() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 103 | .setTitle("Archived Ticket") |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 104 | .setDescription( |
| 105 | `This ticket has been Archived. Type ${getCommandMentionByName( |
| 106 | "ticket/close" |
| 107 | )} to delete it.\n` + hasPremium |
| 108 | ? "Creating a transcript will delete all messages in this ticket" + |
| 109 | `\n\nFor more info on transcripts, check ${getCommandMentionByName("privacy")}` |
| 110 | : "" |
| 111 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | .setStatus("Warning") |
| 113 | .setEmoji("GUILD.TICKET.ARCHIVED") |
| 114 | ], |
| 115 | components: [ |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 116 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 118 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 119 | .setLabel("Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 120 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | .setCustomId("closeticket") |
| 122 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 123 | ].concat( |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 124 | hasPremium |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 125 | ? [ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 126 | new ButtonBuilder() |
| 127 | .setLabel("Create Transcript and Delete") |
| 128 | .setStyle(ButtonStyle.Primary) |
| 129 | .setCustomId("createtranscript") |
| 130 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
| 131 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | : [] |
| 133 | ) |
| 134 | ) |
| 135 | ] |
| 136 | }); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 137 | const data = { |
| 138 | meta: { |
| 139 | type: "ticketClosed", |
| 140 | displayName: "Ticket Archived", |
| 141 | calculateType: "ticketUpdate", |
| 142 | color: NucleusColors.yellow, |
| 143 | emoji: "GUILD.TICKET.ARCHIVED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 144 | timestamp: Date.now() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 145 | }, |
| 146 | list: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 147 | ticketFor: entry(uID!, renderUser((await interaction.guild.members.fetch(uID!)).user)), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 148 | archivedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 149 | archived: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 150 | ticketChannel: entry(channel.id, renderChannel(channel)) |
| 151 | }, |
| 152 | hidden: { |
| 153 | guild: interaction.guild.id |
| 154 | } |
| 155 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 156 | await log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 157 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 158 | return; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 159 | } |
| 160 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 161 | async function purgeByUser(member: string, guild: string) { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 162 | const config = await client.database.guilds.read(guild); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 163 | const fetchedGuild = await client.guilds.fetch(guild); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 164 | if (!config.tickets.category) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 165 | const tickets: CategoryChannel | TextChannel | undefined = fetchedGuild.channels.cache.get( |
| 166 | config.tickets.category |
| 167 | ) as CategoryChannel | TextChannel | undefined; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 168 | if (!tickets) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 169 | let deleted = 0; |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 170 | if (tickets.type === Discord.ChannelType.GuildCategory) { |
| 171 | // For channels, the topic is the user ID, then the word Active |
| 172 | const category = tickets as Discord.CategoryChannel; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 173 | for (const [_id, element] of category.children.cache) { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 174 | if (!(element.type === Discord.ChannelType.GuildText)) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 175 | if (!((element as Discord.TextChannel).topic ?? "").includes(member)) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 176 | try { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 177 | await element.delete(); |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 178 | deleted++; |
| 179 | } catch (e) { |
| 180 | console.error(e); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 181 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 182 | }; |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 183 | } else { |
| 184 | // For threads, the name is the users name, id, then the word Active |
| 185 | const channel = tickets as Discord.TextChannel; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 186 | for (const [_id, element] of channel.threads.cache) { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 187 | if (!element.name.includes(member)) return; |
| 188 | try { |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 189 | await element.delete(); |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 190 | deleted++; |
| 191 | } catch (e) { |
| 192 | console.error(e); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 193 | } |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 194 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 195 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 196 | if (!deleted) return; |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 197 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 198 | const data = { |
| 199 | meta: { |
| 200 | type: "ticketPurge", |
| 201 | displayName: "Tickets Purged", |
| 202 | calculateType: "ticketUpdate", |
| 203 | color: NucleusColors.red, |
| 204 | emoji: "GUILD.TICKET.DELETE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 205 | timestamp: Date.now() |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 206 | }, |
| 207 | list: { |
| 208 | ticketFor: entry(member, renderUser(member)), |
| 209 | deletedBy: entry(null, "Member left server"), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 210 | deleted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 211 | ticketsDeleted: deleted |
| 212 | }, |
| 213 | hidden: { |
| 214 | guild: guild |
| 215 | } |
| 216 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 217 | await log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 218 | } |
| 219 | |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 220 | export { purgeByUser }; |