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