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