node.js - 在 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 机器人将创建的频道分配给指定的类别?
解决方案
您需要使用.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,您将获得更好的性能。
推荐阅读
- cypress - 赛普拉斯:401 由于重定向添加到应用程序(相同的超级域)
- rust - Rust Geo Crate:在 wkt::Geometry 和 geo:Geometry 之间转换
- build - 如何使用 prisma orm 构建电子应用程序?
- opengl - Object 这个概念如何将自身与 glGenBuffers、glBindBuffer、glBufferData 等其他概念联系起来。ETC?
- jhipster - 确保使用 Introspected 注释类
- powershell - 以“SYSTEM”身份运行时,Get-ChildItem 返回访问被拒绝错误
- java - 如何在 JMenu 中添加滚动条或任何允许用户浏览 JMenu 中超出屏幕的所有元素的内容?
- angular - 随机用户名方法的角度打字稿编写测试
- scala - 如何获得 scala 项目的 SonarQube 代码覆盖率
- powershell - .Net5.0 控制台应用程序中的 Powershell ExchangeOnline