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) { |
| 7 | const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger |
| 8 | |
| 9 | let 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 | let channel = (interaction.channel as Discord.TextChannel) |
| 13 | if (!channel.parent || config.tickets.category != channel.parent.id || (thread ? (threadChannel.parent.parent.id != config.tickets.category) : false)) { |
| 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 | } |
| 21 | let status = channel.topic.split(" ")[1]; |
| 22 | if (status == "Archived") { |
| 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 | ]}); |
| 29 | let data = { |
| 30 | meta:{ |
| 31 | type: 'ticketDeleted', |
| 32 | displayName: 'Ticket Deleted', |
| 33 | calculateType: "ticketUpdate", |
| 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 | 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 | } |
| 46 | } |
| 47 | log(data); |
| 48 | interaction.channel.delete(); |
| 49 | return; |
| 50 | } else if (status == "Active") { |
| 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 | ]}); |
| 57 | let overwrites = [ |
| 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[]; |
| 69 | if (config.tickets.supportRole != null) { |
| 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" |
| 74 | }) |
| 75 | } |
| 76 | channel.edit({permissionOverwrites: overwrites}) |
| 77 | channel.setTopic(`${channel.topic.split(" ")[0]} Archived`); |
| 78 | let data = { |
| 79 | meta:{ |
| 80 | type: 'ticketClosed', |
| 81 | displayName: 'Ticket Closed', |
| 82 | calculateType: "ticketUpdate", |
| 83 | color: NucleusColors.yellow, |
| 84 | emoji: 'GUILD.TICKET.ARCHIVED', |
| 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())), |
| 91 | ticketChannel: entry(channel.id, renderChannel(channel)), |
| 92 | }, |
| 93 | hidden: { |
| 94 | guild: interaction.guild.id |
| 95 | } |
| 96 | } |
| 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")) |
| 110 | ].concat(client.database.premium.hasPremium(interaction.guild.id) ? [ |
| 111 | new MessageButton() |
| 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) { |
| 123 | let config = await client.database.guilds.read(guild.id); |
| 124 | if (!config.tickets.category) return; |
| 125 | let tickets = guild.channels.cache.get(config.tickets.category); |
| 126 | if (!tickets) return; |
| 127 | let ticketChannels = tickets.children; |
| 128 | let deleted = 0 |
| 129 | ticketChannels.forEach(element => { |
| 130 | if (element.type != "GUILD_TEXT") return; |
| 131 | if (element.topic.split(" ")[0] == member) { |
| 132 | try { element.delete(); } catch {} |
| 133 | deleted++ |
| 134 | } |
| 135 | }); |
| 136 | if (deleted) { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame^] | 137 | const { log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger |
| 138 | let data = { |
| 139 | meta:{ |
| 140 | type: 'ticketPurge', |
| 141 | displayName: 'Tickets Purged', |
| 142 | calculateType: "ticketUpdate", |
| 143 | color: NucleusColors.red, |
| 144 | emoji: 'GUILD.TICKET.DELETE', |
| 145 | timestamp: new Date().getTime() |
| 146 | }, |
| 147 | list: { |
| 148 | ticketFor: entry(member, renderUser(member)), |
| 149 | deletedBy: entry(null, "Member left server"), |
| 150 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 151 | ticketsDeleted: deleted, |
| 152 | }, |
| 153 | hidden: { |
| 154 | guild: guild.id |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 155 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame^] | 156 | } |
| 157 | log(data); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | export { purgeByUser } |