| import Discord, { MessageButton, MessageActionRow } from "discord.js"; |
| import client from "../../utils/client.js"; |
| import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| import getEmojiByName from "../../utils/getEmojiByName.js"; |
| |
| export default async function (interaction) { |
| const { |
| log, |
| NucleusColors, |
| entry, |
| renderUser, |
| renderChannel, |
| renderDelta |
| } = client.logger; |
| |
| const config = await client.database.guilds.read(interaction.guild.id); |
| let thread = false; |
| if (interaction.channel instanceof Discord.ThreadChannel) thread = true; |
| const threadChannel = interaction.channel as Discord.ThreadChannel; |
| const channel = interaction.channel as Discord.TextChannel; |
| if ( |
| !channel.parent || |
| config.tickets.category !== channel.parent.id || |
| (thread |
| ? threadChannel.parent.parent.id !== config.tickets.category |
| : false) |
| ) { |
| return interaction.reply({ |
| embeds: [ |
| new EmojiEmbed() |
| .setTitle("Deleting Ticket...") |
| .setDescription( |
| "This ticket is not in your tickets category, so cannot be deleted. You cannot run close in a thread." |
| ) |
| .setStatus("Danger") |
| .setEmoji("CONTROL.BLOCKCROSS") |
| ], |
| ephemeral: true |
| }); |
| } |
| const status = channel.topic.split(" ")[1]; |
| if (status === "Archived") { |
| await interaction.reply({ |
| embeds: [ |
| new EmojiEmbed() |
| .setTitle("Delete Ticket") |
| .setDescription("Your ticket is being deleted...") |
| .setStatus("Danger") |
| .setEmoji("GUILD.TICKET.CLOSE") |
| ] |
| }); |
| const data = { |
| meta: { |
| type: "ticketDeleted", |
| displayName: "Ticket Deleted", |
| calculateType: "ticketUpdate", |
| color: NucleusColors.red, |
| emoji: "GUILD.TICKET.CLOSE", |
| timestamp: new Date().getTime() |
| }, |
| list: { |
| ticketFor: entry( |
| channel.topic.split(" ")[0], |
| renderUser( |
| ( |
| await interaction.guild.members.fetch( |
| channel.topic.split(" ")[0] |
| ) |
| ).user |
| ) |
| ), |
| deletedBy: entry( |
| interaction.member.user.id, |
| renderUser(interaction.member.user) |
| ), |
| deleted: entry( |
| new Date().getTime(), |
| renderDelta(new Date().getTime()) |
| ) |
| }, |
| hidden: { |
| guild: interaction.guild.id |
| } |
| }; |
| log(data); |
| interaction.channel.delete(); |
| return; |
| } else if (status === "Active") { |
| await interaction.reply({ |
| embeds: [ |
| new EmojiEmbed() |
| .setTitle("Close Ticket") |
| .setDescription("Your ticket is being closed...") |
| .setStatus("Warning") |
| .setEmoji("GUILD.TICKET.ARCHIVED") |
| ] |
| }); |
| const overwrites = [ |
| { |
| id: channel.topic.split(" ")[0], |
| deny: ["VIEW_CHANNEL"], |
| type: "member" |
| }, |
| { |
| id: interaction.guild.id, |
| deny: ["VIEW_CHANNEL"], |
| type: "role" |
| } |
| ] as Discord.OverwriteResolvable[]; |
| if (config.tickets.supportRole !== null) { |
| overwrites.push({ |
| id: interaction.guild.roles.cache.get( |
| config.tickets.supportRole |
| ), |
| allow: [ |
| "VIEW_CHANNEL", |
| "SEND_MESSAGES", |
| "ATTACH_FILES", |
| "ADD_REACTIONS", |
| "READ_MESSAGE_HISTORY" |
| ], |
| type: "role" |
| }); |
| } |
| channel.edit({ permissionOverwrites: overwrites }); |
| channel.setTopic(`${channel.topic.split(" ")[0]} Archived`); |
| const data = { |
| meta: { |
| type: "ticketClosed", |
| displayName: "Ticket Closed", |
| calculateType: "ticketUpdate", |
| color: NucleusColors.yellow, |
| emoji: "GUILD.TICKET.ARCHIVED", |
| timestamp: new Date().getTime() |
| }, |
| list: { |
| ticketFor: entry( |
| channel.topic.split(" ")[0], |
| renderUser( |
| ( |
| await interaction.guild.members.fetch( |
| channel.topic.split(" ")[0] |
| ) |
| ).user |
| ) |
| ), |
| closedBy: entry( |
| interaction.member.user.id, |
| renderUser(interaction.member.user) |
| ), |
| closed: entry( |
| new Date().getTime(), |
| renderDelta(new Date().getTime()) |
| ), |
| ticketChannel: entry(channel.id, renderChannel(channel)) |
| }, |
| hidden: { |
| guild: interaction.guild.id |
| } |
| }; |
| log(data); |
| await interaction.editReply({ |
| embeds: [ |
| new EmojiEmbed() |
| .setTitle("Close Ticket") |
| .setDescription( |
| "This ticket has been closed.\nType `/ticket close` again to delete it.\n\nNote: Check `/privacy` for details about transcripts." |
| ) |
| .setStatus("Warning") |
| .setEmoji("GUILD.TICKET.ARCHIVED") |
| ], |
| components: [ |
| new MessageActionRow().addComponents( |
| [ |
| new MessageButton() |
| .setLabel("Delete") |
| .setStyle("DANGER") |
| .setCustomId("closeticket") |
| .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| ].concat( |
| client.database.premium.hasPremium(interaction.guild.id) |
| ? [ |
| new MessageButton() |
| .setLabel("Create Transcript and Delete") |
| .setStyle("PRIMARY") |
| .setCustomId("createtranscript") |
| .setEmoji( |
| getEmojiByName( |
| "CONTROL.DOWNLOAD", |
| "id" |
| ) |
| ) |
| ] |
| : [] |
| ) |
| ) |
| ] |
| }); |
| return; |
| } |
| } |
| |
| async function purgeByUser(member, guild) { |
| const config = await client.database.guilds.read(guild.id); |
| if (!config.tickets.category) return; |
| const tickets = guild.channels.cache.get(config.tickets.category); |
| if (!tickets) return; |
| const ticketChannels = tickets.children; |
| let deleted = 0; |
| ticketChannels.forEach((element) => { |
| if (element.type !== "GUILD_TEXT") return; |
| if (element.topic.split(" ")[0] === member) { |
| try { |
| element.delete(); |
| } catch { |
| /* Errors if the channel does not exist (deleted already) */ |
| } |
| deleted++; |
| } |
| }); |
| if (deleted) { |
| const { log, NucleusColors, entry, renderUser, renderDelta } = |
| member.client.logger; |
| const data = { |
| meta: { |
| type: "ticketPurge", |
| displayName: "Tickets Purged", |
| calculateType: "ticketUpdate", |
| color: NucleusColors.red, |
| emoji: "GUILD.TICKET.DELETE", |
| timestamp: new Date().getTime() |
| }, |
| list: { |
| ticketFor: entry(member, renderUser(member)), |
| deletedBy: entry(null, "Member left server"), |
| deleted: entry( |
| new Date().getTime(), |
| renderDelta(new Date().getTime()) |
| ), |
| ticketsDeleted: deleted |
| }, |
| hidden: { |
| guild: guild.id |
| } |
| }; |
| log(data); |
| } |
| } |
| |
| export { purgeByUser }; |