I have been working on my ticket system for my bot, and most users wanted me to put the ticket channels
that were being created into a category, so I've been trying to do just that and I'm running into an issue where it creates a ticket category
and successfully places the opened ticket into the category, but whenever tickets were being created it was also creating a new category for the new tickets.
basically I wanted to know how I can make it create one category and all new tickets will go into that category, and if their is no category.. then create one and put the new tickets into it.
const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const client = require("../index");
client.on("interactionCreate", async interaction => {
if (interaction.isButton()) {
if (interaction.customId === "tic") {
let ticketChannel = interaction.guild.channels.cache.find(ch => ch.name === `ticket-${interaction.user.id}`);
let categoryChannel = interaction.guild.channels.cache.find(ca => ca.name === "tickets")
if (interaction.guild.channels.cache.find(ch => ch.name === `ticket-${interaction.user.id}`)) {
return interaction.reply({ content: `<:CL_Support:912055272275599380> You currently have an open ticket.\n<:CL_Reply:909436090413363252> <#${ticketChannel.id}>`, ephemeral: true });
}
let ticketCategory = interaction.guild.channels.cache.find(ch => ch.name === 'tickets')
if (interaction.guild.channels.cache.find(type => type.type === 'GUILD_CATEGORY' && type.name === 'tickets')) {} else {
const ticketCategory = interaction.guild.channels.create("tickets", {
permissionOverwrites: [
{
id: interaction.guild.me.roles.highest,
allow: ["SEND_MESSAGES", "VIEW_CHANNEL", "ATTACH_FILES", "MANAGE_CHANNELS"]
},
{
id: interaction.user.id,
allow: ["SEND_MESSAGES", "VIEW_CHANNEL", "ATTACH_FILES"]
},
{
id: interaction.guild.roles.everyone,
deny: ["SEND_MESSAGES", "VIEW_CHANNEL", "ATTACH_FILES"]
}
],
type: "GUILD_CATEGORY"
}).catch()
}
const channel = await interaction.guild.channels.create(`ticket-${interaction.user.id}`, {
//parent: "714282800156639384",
parent: ticketCategory, //interaction.guild.channels.cache.find(ch => ch.name === 'tickets')
topic: "Ticketing made easy, right at your fingertips!",
permissionOverwrites: [
{
id: interaction.guild.me.roles.highest,
allow: ["SEND_MESSAGES", "VIEW_CHANNEL", "ATTACH_FILES", "MANAGE_CHANNELS"]
},
{
id: interaction.user.id,
allow: ["SEND_MESSAGES", "VIEW_CHANNEL", "ATTACH_FILES"]
},
{
id: interaction.guild.roles.everyone,
deny: ["SEND_MESSAGES", "VIEW_CHANNEL", "ATTACH_FILES"]
}
],
type: "GUILD_TEXT"
}).catch();
const ticketEmbed = new MessageEmbed()
.setTitle("__Support Ticket__")
.setDescription("> Support will be with you shortly. While you wait, please let us know how we can help you today!\n\nuse `c!close` to close this ticket.") //Click on the 🗑️ to close this ticket")
.setColor("GREEN")
.setFooter(`User ID: ${interaction.user.id}`, interaction.user.displayAvatarURL())
.setTimestamp();
//const deleteTicket = new MessageActionRow().addComponents(
//new MessageButton()
//.setCustomId("delete")
//.setLabel("🗑️ Close")
//.setStyle("DANGER")
//);
interaction.reply({ content: `<:CL_Support:912055272275599380> Your ticket has been created!\n<:CL_Reply:909436090413363252> <#${channel.id}>`, ephemeral: true });
channel.send({ content: `@here <@${interaction.user.id}>`, embeds: [ticketEmbed], }); //components: [deleteTicket] });
const logchannel = interaction.guild.channels.cache.find(channel => channel.name === "ticket-logs");
if (logchannel) {
const ticketLogs = new MessageEmbed()
.setTitle("__Ticket Info__")
.setDescription(`> A new ticket has been created.\n<:CL_Reply:909436090413363252> ${channel.name}`)
.addField("Opened by", `<:CL_Reply:909436090413363252> ${interaction.user.tag} (${interaction.user.id})\n `)
.addField("Created", `<:CL_Reply:909436090413363252> <t:${Math.floor(Date.now() / 1000)}:R>`)
.setFooter("Crimson - Ticket Logger", client.user.displayAvatarURL())
.setColor("#5865F2")
.setTimestamp();
logchannel.send({ embeds: [ticketLogs] });
}
}
}
});
You can solve this problem by changing the way you make a new category. So what I mean is you could detect if the category already exists and if it does, well... don't make a new one!
You can detect if the category exists using this code:
if (interaction.guild.channels.cache.find(type => type.type === 'GUILD_CATEGORY' && type.name === 'tickets')) {
console.log("Hey! We found the tickets category, no need to make one now.")
} else {
// [Insert code to make category here]
}