【问题标题】:How to prevent creating more than one category of the same name?如何防止创建多个同名类别?
【发布时间】:2021-12-31 20:14:00
【问题描述】:

我一直在为我的机器人开发票务系统,大多数用户希望我将正在创建的 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] });
       }
     }
   }
});

【问题讨论】:

    标签: node.js discord.js ticket-system


    【解决方案1】:

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

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

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

    【讨论】:

    • HeyHoo,按预期工作.. 但是要创建的第一个工单被移到顶部(不在类别中),并且类别在底部创建自己。如果我关闭类别之外的一个并制作一个新的,它将被放入类别中。我会用你给我的更新上面的代码。
    • 当需要布尔值时最好使用.some() 而不是.find()
    • 这样吗? if (interaction.guild.channels.cache.some()...
    • @RomanBeard 是的,但请确保正确关闭 if 语句
    • @RomanBeard 如果我的解决方案有帮助,请单击它旁边的复选标记 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多