|
| 1 | +const { |
| 2 | + EmbedBuilder, |
| 3 | + SlashCommandBuilder, |
| 4 | + PermissionFlagsBits, |
| 5 | +} = require("discord.js"); |
| 6 | +const fs = require("fs"); |
| 7 | +const yaml = require("yaml"); |
| 8 | +const configFile = fs.readFileSync("./config.yml", "utf8"); |
| 9 | +const config = yaml.parse(configFile); |
| 10 | +const { |
| 11 | + client, |
| 12 | + ticketsDB, |
| 13 | + mainDB, |
| 14 | + ticketCategories, |
| 15 | + sanitizeInput, |
| 16 | + logMessage, |
| 17 | + checkSupportRole, |
| 18 | +} = require("../../index.js"); |
| 19 | + |
| 20 | +module.exports = { |
| 21 | + enabled: config.commands.reopen.enabled, |
| 22 | + data: new SlashCommandBuilder() |
| 23 | + .setName("reopen") |
| 24 | + .setDescription("Re-Open a closed ticket.") |
| 25 | + .setDefaultMemberPermissions( |
| 26 | + PermissionFlagsBits[config.commands.reopen.permission], |
| 27 | + ) |
| 28 | + .setDMPermission(false), |
| 29 | + async execute(interaction) { |
| 30 | + if (!(await ticketsDB.has(interaction.channel.id))) { |
| 31 | + return interaction.reply({ |
| 32 | + content: config.errors.not_in_a_ticket, |
| 33 | + ephemeral: true, |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + if ((await ticketsDB.get(`${interaction.channel.id}.status`)) === "Open") { |
| 38 | + return interaction.reply({ |
| 39 | + content: "This ticket is already open!", |
| 40 | + ephemeral: true, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + const hasSupportRole = await checkSupportRole(interaction); |
| 45 | + if (!hasSupportRole) { |
| 46 | + return interaction.reply({ |
| 47 | + content: config.errors.not_allowed, |
| 48 | + ephemeral: true, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + let ticketUserID = client.users.cache.get( |
| 53 | + await ticketsDB.get(`${interaction.channel.id}.userID`), |
| 54 | + ); |
| 55 | + let ticketChannel = interaction.guild.channels.cache.get( |
| 56 | + interaction.channel.id, |
| 57 | + ); |
| 58 | + let ticketButton = await ticketsDB.get(`${interaction.channel.id}.button`); |
| 59 | + |
| 60 | + const logEmbed = new EmbedBuilder() |
| 61 | + .setColor(config.commands.reopen.LogEmbed.color) |
| 62 | + .setTitle(config.commands.reopen.LogEmbed.title) |
| 63 | + .addFields([ |
| 64 | + { |
| 65 | + name: config.commands.reopen.LogEmbed.field_staff, |
| 66 | + value: `> <@!${interaction.user.id}>\n> ${sanitizeInput(interaction.user.tag)}`, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: config.commands.reopen.LogEmbed.field_user, |
| 70 | + value: `> <@!${ticketUserID.id}>\n> ${sanitizeInput(ticketUserID.tag)}`, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: config.commands.reopen.LogEmbed.field_ticket, |
| 74 | + value: `> #${sanitizeInput(interaction.channel.name)}\n> ${await ticketsDB.get(`${interaction.channel.id}.ticketType`)}`, |
| 75 | + }, |
| 76 | + ]) |
| 77 | + .setTimestamp() |
| 78 | + .setThumbnail( |
| 79 | + interaction.user.displayAvatarURL({ |
| 80 | + format: "png", |
| 81 | + dynamic: true, |
| 82 | + size: 1024, |
| 83 | + }), |
| 84 | + ) |
| 85 | + .setFooter({ |
| 86 | + text: `${interaction.user.tag}`, |
| 87 | + iconURL: `${interaction.user.displayAvatarURL({ format: "png", dynamic: true, size: 1024 })}`, |
| 88 | + }); |
| 89 | + |
| 90 | + let logsChannel = interaction.guild.channels.cache.get( |
| 91 | + config.logs_channel_id, |
| 92 | + ); |
| 93 | + await logsChannel.send({ embeds: [logEmbed] }); |
| 94 | + |
| 95 | + const embed = new EmbedBuilder() |
| 96 | + .setColor(config.commands.reopen.embed.color) |
| 97 | + .setDescription( |
| 98 | + `${config.commands.reopen.embed.description}` |
| 99 | + .replace(/\{user\}/g, `${interaction.user}`) |
| 100 | + .replace(/\{user\.tag\}/g, sanitizeInput(interaction.user.tag)), |
| 101 | + ); |
| 102 | + |
| 103 | + Object.keys(ticketCategories).forEach(async (id) => { |
| 104 | + if (ticketButton === id) { |
| 105 | + const category = ticketCategories[id]; |
| 106 | + |
| 107 | + if ( |
| 108 | + category.closedCategoryID && |
| 109 | + ticketChannel.parentId !== category.categoryID |
| 110 | + ) { |
| 111 | + await ticketChannel.setParent(category.categoryID, { |
| 112 | + lockPermissions: false, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + category.support_role_ids.forEach(async (roleId) => { |
| 117 | + await interaction.channel.permissionOverwrites.create(roleId, { |
| 118 | + ViewChannel: true, |
| 119 | + SendMessages: true, |
| 120 | + AttachFiles: true, |
| 121 | + EmbedLinks: true, |
| 122 | + ReadMessageHistory: true, |
| 123 | + }); |
| 124 | + }); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + let claimUser = client.users.cache.get( |
| 129 | + await ticketsDB.get(`${interaction.channel.id}.claimUser`), |
| 130 | + ); |
| 131 | + await interaction.channel.permissionOverwrites.create(ticketUserID.id, { |
| 132 | + ViewChannel: true, |
| 133 | + SendMessages: true, |
| 134 | + AttachFiles: true, |
| 135 | + EmbedLinks: true, |
| 136 | + ReadMessageHistory: true, |
| 137 | + }); |
| 138 | + if (claimUser) |
| 139 | + await interaction.channel.permissionOverwrites.create(claimUser.id, { |
| 140 | + ViewChannel: true, |
| 141 | + SendMessages: true, |
| 142 | + AttachFiles: true, |
| 143 | + EmbedLinks: true, |
| 144 | + ReadMessageHistory: true, |
| 145 | + }); |
| 146 | + |
| 147 | + await interaction.channel.messages |
| 148 | + .fetch(await ticketsDB.get(`${interaction.channel.id}.closeMsgID`)) |
| 149 | + .then((msg) => msg.delete()); |
| 150 | + await ticketsDB.set(`${interaction.channel.id}.status`, "Open"); |
| 151 | + await mainDB.push("openTickets", interaction.channel.id); |
| 152 | + await interaction.reply({ embeds: [embed] }); |
| 153 | + logMessage( |
| 154 | + `${interaction.user.tag} re-opened the ticket #${interaction.channel.name} which was created by ${ticketUserID.tag}`, |
| 155 | + ); |
| 156 | + }, |
| 157 | +}; |
0 commit comments