首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建文本通道时出现的discord.js问题

创建文本通道时出现的discord.js问题
EN

Stack Overflow用户
提问于 2019-02-21 03:20:05
回答 1查看 1.7K关注 0票数 0

今天我创建了一个使用"!new“命令的机器人,但后来我遇到了这个问题。"!new“命令创建了一个名为"support-1”的通道,但是当您重新输入"!new“时,它会再次创建一个同名的通道。现在我有一个问题:如何创建一个命令来创建以升序编号的"support“通道?( "support-1”、"support-2“等)我的代码:

代码语言:javascript
复制
message.guild.createChannel(`ticket-${message.author.id}`, "text")

问题是我不知道做ir,所以它按照从0到∞的顺序创建通道!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-22 22:40:36

如果你真的想要正确地做到这一点,你可以从一个行会中获取所有的通道。过滤掉不是文本通道的通道。从该集合中,过滤掉不具有名称support-(number)的通道。从剩下的集合中,找到具有最高编号的通道,并使用该编号+ 1创建一个新通道。

下面是一些示例代码。对其进行测试以确保其正常工作。

代码语言:javascript
复制
bot.on("message", async message => {
  if (message.author.bot) return;

  if (message.content.startsWith('!new')) {
    // Fetch all the channels in the guild.
    let allChannels = message.guild.channels;

    // Filter out all the non-text channels.
    let textChannels = allChannels.filter((channel) => {
      return channel.type === "text";    
    });

    // Filter out all the text channels whose name isn't 'support-(number)'.
    let supportChannels = textChannels.filter((textChannel) => {
      // Checks whether a channel name has format 'support-(number)'. Look into Regex for more info.
      return textChannel.name.match(/^(support-)\d+$/g);
    });

    // Check if there are any support channels.
    if (supportChannels.length) {
      // Get the numbers from the channel name.
      let numbers = supportChannels.map((supportChannel) => {
        return parseInt(supportChannel.name.split('-')[1]);
      });

      // Get the highest number from the array.
      let highestNumber = Math.max(...numbers);

      // Create a new support channel with the highest number + 1.
      message.guild.createChannel(`support-${highestNumber+1}`, 'text');
    } else {
      // There are no support channels, thus create the first one.
      message.guild.createChannel('support-1', 'text');
    }
  }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54793783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档