如何防止创建多个同名类别?

How to prevent creating more than one category of the same name?

我一直在为我的机器人开发票务系统,大多数用户希望我将正在创建的 ticket channels 放入一个类别中,所以我一直在努力做到这一点并且我我 运行 遇到了一个问题,它创建了一个 ticket category 并成功地将打开的工单放入类别中,但每当创建工单时,它也会为新工单创建一个新类别。

基本上我想知道如何让它创建一个类别并且所有新门票都将归入该类别,如果他们没有类别..然后创建一个并将新门票放入其中。

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] });
       }
     }
   }
});

您可以通过更改创建新类别的方式来解决此问题。所以我的意思是您可以检测该类别是否已经存在,如果存在,那么...不要创建新类别!

您可以使用以下代码检测类别是否存在:

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]
}