PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 1 | import Discord, { ButtonBuilder, ActionRowBuilder, ButtonStyle, ButtonInteraction } from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 2 | import client from "../../utils/client.js"; |
| 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 4 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 5 | |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 6 | export default async function (interaction: Discord.CommandInteraction | ButtonInteraction) { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 7 | if (!interaction.guild) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 8 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 9 | |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 10 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 11 | let thread = false; |
| 12 | if (interaction.channel instanceof Discord.ThreadChannel) thread = true; |
| 13 | const threadChannel = interaction.channel as Discord.ThreadChannel; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | const channel = interaction.channel as Discord.TextChannel; |
| 15 | if ( |
| 16 | !channel.parent || |
| 17 | config.tickets.category !== channel.parent.id || |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 18 | (thread ? threadChannel.parent.parent.id !== config.tickets.category : false) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | ) { |
| 20 | return interaction.reply({ |
| 21 | embeds: [ |
| 22 | new EmojiEmbed() |
| 23 | .setTitle("Deleting Ticket...") |
| 24 | .setDescription( |
| 25 | "This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread." |
| 26 | ) |
| 27 | .setStatus("Danger") |
| 28 | .setEmoji("CONTROL.BLOCKCROSS") |
| 29 | ], |
| 30 | ephemeral: true |
| 31 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 32 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 33 | const status = channel.topic.split(" ")[1]; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 34 | if (status === "Archived") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | await interaction.reply({ |
| 36 | embeds: [ |
| 37 | new EmojiEmbed() |
| 38 | .setTitle("Delete Ticket") |
| 39 | .setDescription("Your ticket is being deleted...") |
| 40 | .setStatus("Danger") |
| 41 | .setEmoji("GUILD.TICKET.CLOSE") |
| 42 | ] |
| 43 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | type: "ticketDeleted", |
| 47 | displayName: "Ticket Deleted", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 48 | calculateType: "ticketUpdate", |
| 49 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | emoji: "GUILD.TICKET.CLOSE", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 51 | timestamp: new Date().getTime() |
| 52 | }, |
| 53 | list: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 54 | ticketFor: entry( |
| 55 | channel.topic.split(" ")[0], |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 56 | renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 57 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 58 | deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 59 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 60 | }, |
| 61 | hidden: { |
| 62 | guild: interaction.guild.id |
| 63 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 65 | log(data); |
| 66 | interaction.channel.delete(); |
| 67 | return; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 68 | } else if (status === "Active") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | await interaction.reply({ |
| 70 | embeds: [ |
| 71 | new EmojiEmbed() |
| 72 | .setTitle("Close Ticket") |
| 73 | .setDescription("Your ticket is being closed...") |
| 74 | .setStatus("Warning") |
| 75 | .setEmoji("GUILD.TICKET.ARCHIVED") |
| 76 | ] |
| 77 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | const overwrites = [ |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 79 | { |
| 80 | id: channel.topic.split(" ")[0], |
| 81 | deny: ["VIEW_CHANNEL"], |
| 82 | type: "member" |
| 83 | }, |
| 84 | { |
| 85 | id: interaction.guild.id, |
| 86 | deny: ["VIEW_CHANNEL"], |
| 87 | type: "role" |
| 88 | } |
| 89 | ] as Discord.OverwriteResolvable[]; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 90 | if (config.tickets.supportRole !== null) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 91 | overwrites.push({ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 92 | id: interaction.guild.roles.cache.get(config.tickets.supportRole), |
| 93 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 94 | type: "role" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 95 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 96 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 97 | channel.edit({ permissionOverwrites: overwrites }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 98 | channel.setTopic(`${channel.topic.split(" ")[0]} Archived`); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 100 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 101 | type: "ticketClosed", |
| 102 | displayName: "Ticket Closed", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 103 | calculateType: "ticketUpdate", |
| 104 | color: NucleusColors.yellow, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 105 | emoji: "GUILD.TICKET.ARCHIVED", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 106 | timestamp: new Date().getTime() |
| 107 | }, |
| 108 | list: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | ticketFor: entry( |
| 110 | channel.topic.split(" ")[0], |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 111 | renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 113 | closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 114 | closed: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 115 | ticketChannel: entry(channel.id, renderChannel(channel)) |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 116 | }, |
| 117 | hidden: { |
| 118 | guild: interaction.guild.id |
| 119 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 121 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 122 | await interaction.editReply({ |
| 123 | embeds: [ |
| 124 | new EmojiEmbed() |
| 125 | .setTitle("Close Ticket") |
| 126 | .setDescription( |
| 127 | "This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts." |
| 128 | ) |
| 129 | .setStatus("Warning") |
| 130 | .setEmoji("GUILD.TICKET.ARCHIVED") |
| 131 | ], |
| 132 | components: [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 133 | new ActionRowBuilder().addComponents( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 134 | [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 135 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 136 | .setLabel("Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 137 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | .setCustomId("closeticket") |
| 139 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 140 | ].concat( |
| 141 | client.database.premium.hasPremium(interaction.guild.id) |
| 142 | ? [ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 143 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | .setLabel("Create Transcript and Delete") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 145 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | .setCustomId("createtranscript") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 147 | .setEmoji(getEmojiByName("CONTROL.DOWNLOAD", "id")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 148 | ] |
| 149 | : [] |
| 150 | ) |
| 151 | ) |
| 152 | ] |
| 153 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | async function purgeByUser(member, guild) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 159 | const config = await client.database.guilds.read(guild.id); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 160 | if (!config.tickets.category) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 161 | const tickets = guild.channels.cache.get(config.tickets.category); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 162 | if (!tickets) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 163 | const ticketChannels = tickets.children; |
| 164 | let deleted = 0; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 165 | ticketChannels.forEach((element) => { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 166 | if (element.type !== "GUILD_TEXT") return; |
| 167 | if (element.topic.split(" ")[0] === member) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 168 | try { |
| 169 | element.delete(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 170 | } catch { |
| 171 | /* Errors if the channel does not exist (deleted already) */ |
| 172 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 173 | deleted++; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 174 | } |
| 175 | }); |
| 176 | if (deleted) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 177 | const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 178 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 179 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 180 | type: "ticketPurge", |
| 181 | displayName: "Tickets Purged", |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 182 | calculateType: "ticketUpdate", |
| 183 | color: NucleusColors.red, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 184 | emoji: "GUILD.TICKET.DELETE", |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 185 | timestamp: new Date().getTime() |
| 186 | }, |
| 187 | list: { |
| 188 | ticketFor: entry(member, renderUser(member)), |
| 189 | deletedBy: entry(null, "Member left server"), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 190 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 191 | ticketsDeleted: deleted |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 192 | }, |
| 193 | hidden: { |
| 194 | guild: guild.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 195 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | }; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 197 | log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 201 | export { purgeByUser }; |