我正在尝试使机器人发挥作用,然后转到命令参数中的指定频道。
该代码将使机器人转到指定的频道,并为机器人刚刚发挥的作用添加权限,这就是问题所在。
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中的指定通道,并为该频道创建角色和覆盖角色权限。
实际输出是机器人可以完成所有功能,但是该角色对通道没有特殊的权限。
您的代码有两个主要问题:
-
Guild.createRole()
不会同步返回Role
:它返回Promise<Role>
,因此您实际上并未提供.overwritePermissions()
参数的角色 - 创建角色(如果将其正确存储在
arole
中(之后,您将无法访问message.arole
。
您可以使用async/await
或使用.then()
Promise方法来执行此操作。
如果您对承诺或异步代码不信心,则应该尝试学习一些有关它的知识,这确实很有用:使用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);