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