const { query, queryMulti } = require("./helper"); const { ticket_settings } = require("../config"); const { ActionRowBuilder, EmbedBuilder, ButtonBuilder, StringSelectMenuBuilder } = require("discord.js"); module.exports = async (client, db) => { client.on("interactionCreate", async interaction => { if (!interaction.customId || interaction.customId !== "create_ticket") { return; } const options = await queryMulti(db, `SELECT * FROM options WHERE guildID='${interaction.guildId}'`); if (options) { await interaction.deferReply({ ephemeral: true }); const selectMenu = new StringSelectMenuBuilder() .setCustomId("newTicket") .setPlaceholder("Choose a reason for the ticket"); options.forEach((option, index) => { selectMenu.addOptions([ { label: option.name, description: option.description || "No reason", value: `newTicket_${option.id}` } ]); }); const actionRow = new ActionRowBuilder().addComponents(selectMenu); return interaction.editReply({ content: "What will be the reason for the ticket?", components: [actionRow], ephemeral: true }); } else { await interaction.deferReply({ ephemeral: true }); const embed = new EmbedBuilder() .setTimestamp() .setColor(ticket_settings.embed_color) .setDescription(ticket_settings.ticket_message.replace('{user}', interaction.user)) .setThumbnail(ticket_settings.ticket_thumbnail) .setAuthor({ name: interaction.user.tag, iconURL: interaction.user.avatarURL() }); const existingTicket = await query( db, `SELECT * FROM tickets WHERE status='open' AND userID='${interaction.user.id}' AND guildID='${interaction.guildId}'` ); const existingChannel = client.channels.cache.get(existingTicket?.channelID); if (existingTicket && existingChannel) { return interaction.editReply({ content: `You already have a ticket! Please check the ${existingChannel}`, ephemeral: true }); } const settings = await query(db, `SELECT * FROM settings WHERE guildID='${interaction.guildId}'`); db.query(`UPDATE settings SET ticket_counter=ticket_counter+1 WHERE guildID='${interaction.guildId}'`); const permissions = [ { id: interaction.guildId, deny: ["ViewChannel", "SendMessages"] }, { id: interaction.member.id, allow: ["ViewChannel", "SendMessages"] }, { id: client.user.id, allow: ["ViewChannel", "SendMessages"] }, { id: settings?.support_roleID, allow: ["ViewChannel", "SendMessages"] } ]; const channel = await interaction.guild.channels.create({ name: `ticket-${pad(settings.ticket_counter + 1, 4)}`, topic: `#Ticket | From ${interaction.user.username}`, permissionOverwrites: permissions }); const closeButton = new ButtonBuilder() .setEmoji(ticket_settings.buttoncloseemoji) .setLabel(ticket_settings.buttoncloselabel) .setStyle(ticket_settings.buttonclosestyle) .setCustomId("close_ticket"); const actionRow = new ActionRowBuilder().addComponents(closeButton); await channel.send({ content: `<@&${settings?.support_roleID}>`, embeds: [embed], components: [actionRow] }); db.query( "INSERT INTO tickets (id, status, userID, guildID, channelID) VALUES (?, ?, ?, ?, ?)", [pad(settings.ticket_counter + 1, 4), 'open', interaction.user.id, interaction.guildId, channel.id] ); return interaction.editReply({ content: `You successfully created a ticket! Please check the ${channel}`, ephemeral: true }); } }); }; function pad(num, size) { let numStr = num.toString(); while (numStr.length < size) { numStr = '0' + numStr; } return numStr; }