javascript 按名称将频道添加到类别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53479542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:12:53  来源:igfitidea点击:

Add channel to category by name

javascriptmovecategorieschanneldiscord.js

提问by 6IU

    var server = message.guild;
    for (var i = 0; i < server.channels.array().length; i++) {
        server.channels.array()[i].delete();
    }

    server.createChannel("Text Channels", "category");
    server.createChannel('general', "text");

I am trying to make the text channel 'general` go into the category 'Text Channels'

我试图让文本频道“通用”进入“文本频道”类别

All the solutionsI have found rely on you knowing the categories id. I was wondering if there is a way I could get the category id, or else move general into "Text Channels" simply by its name.

我找到的所有解决方案依赖于您了解类别 id。我想知道是否有一种方法可以获得类别 id,或者仅通过其名称将一般移动到“文本频道”。

NOTE:: Currently I am thinking of something along these lines to get the category id:

注意::目前我正在考虑按照以下方式获取类别 ID:

var categoryID = server.categories.find("name","Text Channels");

Then to use

然后使用

server.channels.find("name","general").setParent(categoryID);

采纳答案by Federico Grandi

You can use GuildChannel.setParent(). Please keep in mind that categories are considered as channels by Discord: CategoryChannelextends GuildChannel, so you can check the type with GuildChannel.type

您可以使用GuildChannel.setParent(). 请记住,Discord: CategoryChannelextends将类别视为频道GuildChannel,因此您可以使用以下命令检查类型GuildChannel.type

To assign an existing channel:

要分配现有频道:

let category = server.channels.find(c => c.name == "Text Channels" && c.type == "category"),
  channel = server.channels.find(c => c.name == "general" && c.type == "text");

if (category && channel) channel.setParent(category.id);
else console.error(`One of the channels is missing:\nCategory: ${!!category}\nChannel: ${!!channel}`);

To create a new channel:

要创建新频道:

server.createChannel("general", "text")
  .then(channel => {
    let category = server.channels.find(c => c.name == "Text Channels" && c.type == "category");

    if (!category) throw new Error("Category channel does not exist");
    channel.setParent(category.id);
  }).catch(console.error);


Edit: discord.js@v12
The only thing that changes is that you have to use the GuildChannelManagerfor everything.

编辑:discord.js@v12
唯一改变的是你必须GuildChannelManager为所有事情使用。

let category = server.channels.cache.find(c => c.name == "Text Channels" && c.type == "category"),
  channel = server.channels.cache.find(c => c.name == "general" && c.type == "text");

if (category && channel) channel.setParent(category.id);
else console.error(`One of the channels is missing:\nCategory: ${!!category}\nChannel: ${!!channel}`);
server.channels.create("general")
  .then(channel => {
    let category = server.channels.cache.find(c => c.name == "Text Channels" && c.type == "category");

    if (!category) throw new Error("Category channel does not exist");
    channel.setParent(category.id);
  }).catch(console.error);