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