pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 1 | import Discord, { MessageButton, MessageActionRow } from "discord.js"; |
| 2 | import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 3 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 4 | import readConfig from "../../utils/readConfig.js"; |
| 5 | |
| 6 | export default async function (interaction) { |
| 7 | // @ts-ignore |
| 8 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = interaction.client.logger |
| 9 | |
| 10 | let config = await readConfig(interaction.guild.id); |
| 11 | let channel = (interaction.channel as Discord.TextChannel) |
pineafan | 5be5eca | 2022-05-19 21:37:08 +0100 | [diff] [blame] | 12 | if (!channel.parent || config.tickets.category != channel.parent.id) { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 13 | return interaction.reply({embeds: [new generateEmojiEmbed() |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 14 | .setTitle("Deleting Ticket...") |
pineafan | 5be5eca | 2022-05-19 21:37:08 +0100 | [diff] [blame] | 15 | .setDescription("This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread.") // TODO bridge to cross later! |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 16 | .setStatus("Danger") |
| 17 | .setEmoji("CONTROL.BLOCKCROSS") |
| 18 | ], ephemeral: true}); |
| 19 | } |
| 20 | let status = channel.topic.split(" ")[1]; |
| 21 | if (status == "Archived") { |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 22 | await interaction.reply({embeds: [new generateEmojiEmbed() |
| 23 | .setTitle("Delete Ticket") |
| 24 | .setDescription("Your ticket is being deleted...") |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 25 | .setStatus("Danger") |
| 26 | .setEmoji("GUILD.TICKET.CLOSE") |
| 27 | ]}); |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 28 | let data = { |
| 29 | meta:{ |
| 30 | type: 'ticketDeleted', |
| 31 | displayName: 'Ticket Deleted', |
| 32 | calculateType: true, |
| 33 | color: NucleusColors.red, |
| 34 | emoji: 'GUILD.TICKET.CLOSE', |
| 35 | timestamp: new Date().getTime() |
| 36 | }, |
| 37 | list: { |
| 38 | ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)), |
| 39 | deletedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 40 | closed: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
| 41 | }, |
| 42 | hidden: { |
| 43 | guild: interaction.guild.id |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 44 | } |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 45 | } |
| 46 | log(data, interaction.client); |
| 47 | interaction.channel.delete(); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 48 | return; |
| 49 | } else if (status == "Active") { |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 50 | await interaction.reply({embeds: [new generateEmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 51 | .setTitle("Close Ticket") |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 52 | .setDescription("Your ticket is being closed...") |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 53 | .setStatus("Warning") |
| 54 | .setEmoji("GUILD.TICKET.ARCHIVED") |
| 55 | ]}); |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 56 | let overwrites = [ |
| 57 | { |
| 58 | id: channel.topic.split(" ")[0], |
| 59 | deny: ["VIEW_CHANNEL"], |
| 60 | type: "member" |
| 61 | }, |
| 62 | { |
| 63 | id: interaction.guild.id, |
| 64 | deny: ["VIEW_CHANNEL"], |
| 65 | type: "role" |
pineafan | 5be5eca | 2022-05-19 21:37:08 +0100 | [diff] [blame] | 66 | } |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 67 | ] as Discord.OverwriteResolvable[]; |
| 68 | if (config.tickets.supportRole != null) { |
| 69 | overwrites.push({ |
| 70 | id: interaction.guild.roles.cache.get(config.tickets.supportRole), |
| 71 | allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"], |
| 72 | type: "role" |
| 73 | }) |
| 74 | } |
| 75 | channel.edit({permissionOverwrites: overwrites}) |
| 76 | channel.setTopic(`${channel.topic.split(" ")[0]} Archived`); |
| 77 | let data = { |
| 78 | meta:{ |
| 79 | type: 'ticketClosed', |
| 80 | displayName: 'Ticket Closed', |
| 81 | calculateType: true, |
| 82 | color: NucleusColors.yellow, |
| 83 | emoji: 'GUILD.TICKET.ARCHIVED', |
| 84 | timestamp: new Date().getTime() |
| 85 | }, |
| 86 | list: { |
| 87 | ticketFor: entry(channel.topic.split(" ")[0], renderUser((await interaction.guild.members.fetch(channel.topic.split(" ")[0])).user)), |
| 88 | closedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 89 | closed: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 90 | ticketChannel: entry(channel.id, renderChannel(channel)), |
| 91 | }, |
| 92 | hidden: { |
| 93 | guild: interaction.guild.id |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 94 | } |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 95 | } |
| 96 | log(data, interaction.client); |
| 97 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 98 | .setTitle("Close Ticket") |
| 99 | .setDescription("This ticket has been closed.\nType `/ticket close` again to delete it.") |
| 100 | .setStatus("Warning") |
| 101 | .setEmoji("GUILD.TICKET.ARCHIVED") // TODO:[Premium] Add a transcript option ||\----/|| <- the bridge we will cross when we come to it |
| 102 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 103 | .setLabel("Delete") |
| 104 | .setStyle("DANGER") |
| 105 | .setCustomId("closeticket") |
| 106 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 107 | ])]}); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | async function purgeByUser(member, guild) { |
| 113 | let config = await readConfig(guild.id); |
| 114 | if (!config.tickets.category) return; |
| 115 | let tickets = guild.channels.cache.get(config.tickets.category); |
pineafan | 5be5eca | 2022-05-19 21:37:08 +0100 | [diff] [blame] | 116 | if (!tickets) return; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 117 | let ticketChannels = tickets.children; |
| 118 | let deleted = 0 |
| 119 | ticketChannels.forEach(element => { |
| 120 | if (element.type != "GUILD_TEXT") return; |
| 121 | if (element.topic.split(" ")[0] == member) { |
| 122 | element.delete(); |
| 123 | deleted++ |
| 124 | } |
| 125 | }); |
| 126 | if (deleted) { |
| 127 | try { |
| 128 | const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger |
| 129 | let data = { |
| 130 | meta:{ |
| 131 | type: 'ticketPurge', |
| 132 | displayName: 'Tickets Purged', |
| 133 | calculateType: true, |
| 134 | color: NucleusColors.red, |
| 135 | emoji: 'GUILD.TICKET.DELETE', |
| 136 | timestamp: new Date().getTime() |
| 137 | }, |
| 138 | list: { |
| 139 | ticketFor: entry(member, renderUser(member)), |
| 140 | deletedBy: entry(null, "Member left server"), |
| 141 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 142 | ticketsDeleted: deleted, |
| 143 | }, |
| 144 | hidden: { |
| 145 | guild: guild.id |
| 146 | } |
| 147 | } |
| 148 | log(data, member.client); |
| 149 | } catch {} |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | export { purgeByUser } |