1

我正在尝试让机器人扮演角色并在命令的参数中转到指定的频道。
该代码将使机器人进入指定的频道,并为机器人刚刚创建的角色添加权限,这就是问题所在。
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 一切正常,但角色没有通道的特殊权限。

4

1 回答 1

0

您的代码有两个主要问题:

  • Guild.createRole()不同步返回 a Role:它返回 a ,因此Promise<Role>您实际上没有提供角色作为参数.overwritePermissions()
  • 创建角色后(如果您将其正确存储在 中arole),您将无法以message.arole.

您可以使用async/await或使用.then()promise 方法来做到这一点。
如果你对 Promise 或异步代码没有信心,你应该尝试学习一些关于它的东西,它真的很有用:查看Using PromisesPromise以及async functionMDN 的文档。

这是一个例子:

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);
于 2019-05-06T17:57:29.810 回答