【问题标题】:How to fix 'Supplied parameter was neither a User or Role.'如何修复“提供的参数既不是用户也不是角色”。
【发布时间】:2019-09-23 19:21:08
【问题描述】:

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

【问题讨论】:

    标签: discord.js commando


    【解决方案1】:

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

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

    您可以使用 async/await 或使用 .then() 承诺方法来做到这一点。
    如果您对 Promise 或异步代码没有信心,您应该尝试了解它,这非常有用:查看 MDN 的 Using promisesPromiseasync 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);
    

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 2020-08-20
      • 1970-01-01
      • 2021-08-13
      • 2021-11-15
      • 2023-01-13
      • 2019-05-13
      • 1970-01-01
      • 2023-03-02
      相关资源
      最近更新 更多