检查频道是否存在的方法 [英] Way to check if a channel exists

查看:66
本文介绍了检查频道是否存在的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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文档以寻求解决方案,但到目前为止,我还没有运气。我需要说明如何实现此目标。

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.

推荐答案

您可以使用 < Guild> .channels ,它返回收藏 ://discord.js.org/#/docs/main/stable/class/GuildChannel rel = nofollow noreferrer> GuildChannel s,可以在此集合中使用 < Colection> .exists() 检查渠道是否已存在于公会中。

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.

是这样的:

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

这篇关于检查频道是否存在的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆