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