首页 > 解决方案 > 在 discord.js 中将频道分配给类别

问题描述

我有一个机器人,它在 Discord 服务器内创建一个频道。我在将频道分配给某个类别时遇到了麻烦。

我的代码:

    if (!message.member.hasPermission("ADMINISTRATOR)")) return message.reply("nope");
if (command === "open") {
if (!args[0]) return message.channel.send('Proper usage: *open <name>');
let botmessage = args.join(" ");
message.guild.createChannel('' + botmessage, { type: 'text' })
channel.setParent('[ID of Category here]')

机器人成功地创建了频道,但它没有分配到类别中。我得到的错误是:

“频道”未定义。

总的来说,我仍在学习 promises 和 discord.js。如何让我的 Discord 机器人将创建的频道分配给指定的类别?

标签: node.jspromisediscorddiscord.js

解决方案


您需要使用.then()来获取已解析的通道:

message.guild.createChannel('' + botmessage, { type: 'text' }).then((channel) => {
    channel.setParent('[ID of Category here]');
});

它会起作用。请注意,您还可以使用父选项创建通道:

message.guild.createChannel('' + botmessage, { type: 'text', parent: '[ID of Category here]' });

通过这种方式,您无需使用 Promise,您将获得更好的性能。


推荐阅读