You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PermissionOverwrites.create()参数正确仍报错:非用户或角色参数

问题解决:Supplied parameter is not a User nor a Role

错误原因

你的代码里将role变量赋值为角色ID字符串,但permissionOverwrites.create()方法不接受纯ID字符串作为目标参数,它需要的是Role对象,或是包含id和type属性的配置对象。直接传ID会导致方法无法识别目标类型,从而抛出错误。

修正方案

方案1:直接使用Role对象(推荐)

不需要提取角色ID,直接使用getRole()返回的完整Role对象:

if (interaction.commandName == "addchannelperms") {
  const permission = interaction.options.get("permission1").value;
  const role = interaction.options.getRole("role"); // 直接获取Role对象
  const channel = interaction.options.getChannel("channel");

  console.log(role.id); // 依然可以打印ID

  channel.permissionOverwrites.create(role, { allow: [permission] })
    .catch(error => console.log(error));
}

方案2:使用ID+类型配置对象

如果必须用ID,需要传入包含id和type的对象,指定类型为"role":

if (interaction.commandName == "addchannelperms") {
  const permission = interaction.options.get("permission1").value;
  const roleId = interaction.options.getRole("role").id;
  const channel = interaction.options.getChannel("channel");

  console.log(roleId);

  channel.permissionOverwrites.create(
    { id: roleId, type: "role" }, // 明确指定ID和类型
    { allow: [permission] }
  ).catch(error => console.log(error));
}

额外注意事项

  • 确保permission是有效的Discord权限值,如果你的命令选项传入的是权限名称字符串(比如"ViewChannel"),需要用PermissionsBitField.resolve()转换为对应数值:
    const permission = PermissionsBitField.resolve(interaction.options.get("permission1").value);
    
  • 确认目标频道存在且机器人有权限修改其权限(你已确认机器人有管理员权限,这一步可以忽略)

内容的提问来源于stack exchange,提问作者Merthjm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 11:43:21