discord.js - 如何修复“提供的参数既不是用户也不是角色”。
问题描述
我正在尝试让机器人扮演角色并在命令的参数中转到指定的频道。
该代码将使机器人进入指定的频道,并为机器人刚刚创建的角色添加权限,这就是问题所在。
VSC 中的控制台说“未指定角色/用户”并跳过它。
我尝试将 更改arole
为 var,并将arole
( message.arole
) 设置为arole.id
,但它仍然不起作用。乱搞和更改设置根本不起作用。
let woaID = message.mentions.channels.first();
if (!woaID) return message.channel.send("Channel is nonexistant or command was not formatted properly. Please do s!woa #(channelname)");
let specifiedchannel = message.guild.channels.find(t => t.id == woaID.id);
var arole = message.guild.createRole({
name: `A marker v1.0`,
color: 0xcc3b3b,
hoist: false,
mentionable: false,
permissions: ['SEND_MESSAGES']
}).catch(console.error);
message.channel.send("Created role...");
message.channel.send("Role set up...");
/*const sbwrID = message.guild.roles.find(`null v1.0`);
let specifiedrole = message.guild.roles.find(r => r.id == sbwrID.id)*/
message.channel.send('Modified');
specifiedchannel.overwritePermissions(message.arole, {
VIEW_CHANNEL: true,
SEND_MESSAGES: false
})
.then(updated => console.log(updated.permissionOverwrites.get(arole.id)))
.catch(console.error);
我希望机器人能够访问 args 中的指定频道,并为该频道创建角色并覆盖角色权限。
实际输出是 bot 一切正常,但角色没有通道的特殊权限。
解决方案
您的代码有两个主要问题:
Guild.createRole()
不同步返回 aRole
:它返回 a ,因此Promise<Role>
您实际上没有提供角色作为参数.overwritePermissions()
- 创建角色后(如果您将其正确存储在 中
arole
),您将无法以message.arole
.
您可以使用async/await
或使用.then()
promise 方法来做到这一点。
如果你对 Promise 或异步代码没有信心,你应该尝试学习一些关于它的东西,它真的很有用:查看Using Promises,Promise
以及async function
MDN 的文档。
这是一个例子:
message.guild.createRole({
name: `A marker v1.0`,
color: 0xcc3b3b,
hoist: false,
mentionable: false,
permissions: ['SEND_MESSAGES']
}).then(async arole => {
let updated = await specifiedchannel.overwritePermissions(arole, {
VIEW_CHANNEL: true,
SEND_MESSAGES: false
});
console.log(updated.permissionOverwrites.get(arole.id));
}).catch(console.error);
推荐阅读
- html - 嵌套树中相对定位元素后面具有绝对位置样式的元素和 z-index 无效
- cm-synergy - IBM Rational Synergy CLI 获取工作区冲突列表
- php - 如何在 php 中显示带有会话的用户图像?
- mongodb - 将 MongoDB 数据导入 Hive 错误:拆分器实现不兼容
- ringcentral - 如何获得 RingCentral 呼叫队列成员可用性?
- d - ZLib 不会在 D 中压缩超过 ~600kb 的输出
- mysql - 如何在sql中获取列的长度属性?
- php - 如何通过 PHP 在带有肥皂标签的 xml 字符串中添加一个孩子
- ajax - Django csrf_token 以一种形式工作,但不能以其他形式工作
- python - For Loop 将数据框列转换为单独的列表