Way to check if a channel exists

后端 未结 3 1240
迷失自我
迷失自我 2021-01-24 22:24
module.exports.run = async (bot, message, args) => {

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

    message.guild.createChannel         


        
相关标签:
3条回答
  • 2021-01-24 22:46

    You can do this by checking for the id. While this might not resolve this particular issue, it might help others with a similar issue.:

    
    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
    
    0 讨论(0)
  • 2021-01-24 22:52

    You can use <Guild>.channels, which returns a collection of GuildChannels, from this collection you can use <Colection>.exists() to check if the channel already exists in the guild.

    So something like this:

    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}...
    
    0 讨论(0)
  • 2021-01-24 22:54

    I do it in the following way, and I go through each of the channels.

    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----
    
    
    0 讨论(0)
提交回复
热议问题