如何修复 'Supplied parameter was neither a User or Role.'

How to fix 'Supplied parameter was neither a User or Role.'

我正在尝试让机器人扮演角色并转到命令参数中的指定频道。
代码会让bot进入指定的频道,并为bot刚刚创建的角色添加权限,这就是问题所在。
VSC 中的控制台显示 "a role / user was not specified" 并跳过它。

我已经尝试将 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() 不会同步 return 一个 Role:它 return 一个 Promise<Role>,所以你实际上没有提供一个角色作为.overwritePermissions()
  • 的参数
  • 创建角色后(如果您将其正确存储在 arole 中),您将无法以 message.arole.
  • 访问它

您可以使用 async/await 或使用 .then() promise 方法。
如果您对 promises 或异步代码没有信心,您应该尝试了解它,它真的很有用:查看 MDN 的 Using promises, the Promise and async function 文档。

这是一个例子:

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);