【问题标题】:Add channel to category by name按名称将频道添加到类别
【发布时间】:2019-04-28 00:07:52
【问题描述】:
    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");

我正在尝试使文本通道“通用”进入类别“文本通道”

所有解决方案我发现依靠您知道类别 ID。我想知道是否有办法获取类别 ID,或者仅通过名称将一般内容移至“文本通道”。

注意:: 目前我正在考虑一些类似的东西来获取类别 ID:

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

然后使用

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

【问题讨论】:

  • 为什么上面提到的代码不起作用?请给我们一个您尝试过的事情的适当示例,以便我可以帮助您。我们不提供代码编写服务。
  • @KoenHollander 代码正在运行,我只是在询问如何将一般信息移入文本频道。我一直无法找到任何有效的地方。如果这被视为垃圾邮件,您是否知道我可以询问的任何网站?
  • 我明白了,对不起。我找到了一些有用的东西,您可以设置频道父级:github.com/discordjs/discord.js/issues/2644
  • @KoenHollander 这似乎工作正常,除了它只适用于频道 ID。如果您知道任何获取类别 ID 的方法,那就太好了。我知道如何获取频道 ID,而不是类别。对不起,如果我在浪费你的时间
  • 我没有看到问题,底部的代码对我来说非常好。

标签: javascript move categories channel discord.js


【解决方案1】:

您可以使用GuildChannel.setParent()。请记住,Discord 将类别视为频道:CategoryChannel extends GuildChannel,因此您可以使用 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);

【讨论】:

  • 代码返回 [][][][] DeprecationWarning: Collection#find: 传递一个函数 [][][][][] 更多信息:pastebin.com/9dy7tNgK
  • 那只是一个DeprecationWarning,错误就在下面:它说通道不存在(执行此代码之前您没有创建通道)。我将更新我的答案以包括频道的创建。
  • 当前 Discord.JS 版本不再提供此功能
  • @MattWestlake 这个答案来自 2018 年,我将使用 discord.ks@v12 的方法对其进行更新
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2021-02-06
    • 2014-10-13
    • 1970-01-01
    • 2014-11-17
    相关资源
    最近更新 更多