按名称将频道添加到类别

Add channel to category by name

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

我正在尝试将文本频道“一般”归入类别 'Text Channels'

我找到的所有解决方案取决于您知道类别 ID。我想知道是否有办法获取类别 ID,或者仅通过其名称将 general 移至 "Text Channels"。

注意:: 目前我正在考虑按照这些思路获取类别 ID:

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

然后使用

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

您可以使用GuildChannel.setParent(). Please keep in mind that categories are considered as channels by Discord: CategoryChannel extends GuildChannel, so you can check the type with GuildChannel.type

要分配现有频道:

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}`);

创建新频道:

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

编辑: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);

我知道为时已晚,但这是获取 channel_id 的方法:

  • 右键单击频道打开菜单
  • 点击复制 ID(在菜单底部)