检查通道是否存在的方法

时间:2018-02-24 12:07:24

标签: javascript discord.js

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文档中搜索几次以获得解决方案,但到目前为止我还没有运气。我需要解释如何实现这一目标。

3 个答案:

答案 0 :(得分:0)

您可以使用<Guild>.channelscollection 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}...

答案 1 :(得分:0)

我以以下方式进行操作,并且遍历了每个渠道。

let nameOfChannel = "lista-" + message.author.username.toLowerCase();

        // Check if channel exist
        if ((message.guild.channels.cache.find(c => c.name.toLowerCase() === nameOfChannel))) {

---- code continue here----

答案 2 :(得分:0)

您可以通过检查ID来做到这一点。 尽管这可能无法解决此特定问题,但可能会帮助其他遇到类似问题的人。


var chan_id;

if(message.guild.channels.cache.get(chan_id) === undefined)  { 
    //checks if the channel doesn't exist
    //put the action to take here
} 
//continue your code here
相关问题