Home > front end >  A new category is being created each time a users clicks on the support button
A new category is being created each time a users clicks on the support button

Time:08-19

I have a ticket system ive been updating from v13 to v14 of discord, and ive changed a lot to meet the v14 reqs, but Im having an issue with the categories part (which wasn't an issue in v13 of discord)

So when I go to create a ticket, I just click the support button and a ticket is made along side the category being created.

Image of the ticket after being created and placed in a brand new category

Code:

const { PermissionsBitField, EmbedBuilder, ChannelType, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonInteraction } = require('discord.js');
const client = require('../index');
const supportSchema = require('../models/supportRole');
const categorySchema = require('../models/category');

client.on('interactionCreate', async (interaction) => {
  try {
    if (interaction.isButton()) {
      if (interaction.customId === 'tic') {
        let ticketChannel = interaction.guild.channels.cache.find((tc) => tc.name === `ticket-${interaction.user.id}`);
        let ticketCategory = interaction.guild.channels.cache.find((ch) => ch.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 });
        if (!interaction.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageChannels)) return interaction.reply({ content: "I do not have perms to create your ticket channel. - `[MANAGE_CHANNELS]`", ephemeral: true });
        
        if (interaction.guild.channels.cache.some((type) => type.type === 'GuildCategory' && type.name === 'tickets')) {
          console.log(`A new ticket has been created in ${interaction.guild.name} by ${interaction.user.username}`);
        } else {
          categorySchema.findOne({ Guild: interaction.guild.id }, async (err, data) => {
              if (data) {
                const ticketCategory = interaction.guild.channels.create({
                    name: `${data.Category}`,
                    permissionOverwrites: [
                      {
                        id: interaction.guild.members.me.roles.highest,
                        allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles, PermissionsBitField.Flags.ManageChannels],
                      },
                      {
                        id: interaction.user.id,
                        allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles],
                      },
                      {
                        id: interaction.guild.roles.everyone,
                        deny: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles],
                      },
                    ],
                    type: ChannelType.GuildCategory,
                  }).catch();
              }
            
              if (!data) {
                const ticketCategory = interaction.guild.channels.create({
                    name: 'tickets',
                    permissionOverwrites: [
                      {
                        id: interaction.guild.members.me.roles.highest,
                        allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles, PermissionsBitField.Flags.ManageChannels],
                      },
                      {
                        id: interaction.user.id,
                        allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles],
                      },
                      {
                        id: interaction.guild.roles.everyone,
                        deny: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles],
                      },
                    ],
                    type: ChannelType.GuildCategory,
                  }).catch();
              }
            }
          );
        }
        
        supportSchema.findOne({ Guild: interaction.guild.id }, async (err, data) => {
            if (data) {
              let role = data.Role;
              const channel = await interaction.guild.channels.create({
                  parent: ticketCategory,
                  name: `ticket-${interaction.user.id}`,
                  topic: "Ticketing made easy, right at your fingertips!",
                  permissionOverwrites: [
                    {
                      id: interaction.guild.members.me.roles.highest,
                      allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles, PermissionsBitField.Flags.ManageChannels],
                    },
                    {
                      id: interaction.user.id,
                      allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles],
                    },
                    {
                      id: interaction.guild.roles.everyone,
                      deny: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles],
                    },
                    {
                      id: role,
                      allow: [PermissionsBitField.Flags.SendMessages, PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.AttachFiles, PermissionsBitField.Flags.ManageChannels],
                    },
                  ],
                  type: ChannelType.GuildText,
                }).catch();
              
              const ticketEmbed = new EmbedBuilder()
                .setTitle('__Support Ticket__')
                .setDescription("> Support will be with you shortly. While you wait, please let us know how we can help you today!\n\nClick on the            
  • Related