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