检查通道是否存在的方法

新手上路,请多包涵
module.exports.run = async (bot, message, args) => {

    let ticketreason = args[1];
    let ticketname = "ticket" + ticketreason;

    message.guild.createChannel("tickets",  "category")
    message.guild.createChannel(ticketname, "text");

}

所以我在这里得到了这个非常简单和基本的代码。我正在尝试找到一种方法来在创建频道之前检查频道是否存在。我曾多次尝试在 discord.js 文档中搜索解决方案,但到目前为止我还没有成功。我需要解释如何实现这一目标。

原文由 Josh Bowker 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 246
1 个回答

您可以使用 <Guild>.channels ,它返回 GuildChannel集合,您可以从这个集合中使用 <Colection>.exists() 检查公会中是否已经存在频道。

所以像这样:

 if (message.guild.channels.exists('name', ticketname)) { //checks if there in an item in the channels collection that corresponds with the supplied parameters, returns a boolean
    message.reply(`The ${ticketname} channel already exists in this guild.`).catch(console.error);
    return; //prevents the rest of the code from being executed
}

// Code that creates the channel {ticketname}...

原文由 newbie 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题