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