Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - Way to check if a channel exists

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");

}

So I've got this very simple and basic code here. I am trying to find a way to check if a channel exists before creating it. I've attempted to search around the discord.js documentation a few times for a solution, but I've had no luck so far. I need an explanation for how I can achieve this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...