Discord.js DiscordAPIError:缺少权限---缺少哪个权限?

时间:2021-04-03 08:50:10

标签: discord discord.js

我有一个机器人,它应该在特定类别下创建一个频道,然后将两个用户添加到该频道。

以下代码“应该”起作用并添加用户 2,但因 DiscordAPIError: Missing Permissions 而失败。 我无法弄清楚这需要实际许可吗?

function addUserToChannel(ch, user, altCh) {
  console.log(user.username,"sendmsg",ch.permissionsFor(client.user).has("MANAGE_CHANNELS", false)); //returns true
  if (!ch.permissionsFor(user).has("SEND_MESSAGES", false)) {
    console.log("User", user.username, "lacks view-channel permission");
    ch.updateOverwrite(user, { //fails here obviously
      SEND_MESSAGES: true,
      VIEW_CHANNEL: true
    });
  } else {
    console.log("WARN:", user.username, " already in channel ", ch.name);
    altCh.send(user.toString() + " join conversation here: " + ch.toString());
  }
}

使用以下代码创建频道:

function createPrivateChannel(guild, convo, user) {
  let everyoneRole = guild.roles.cache.find(r => r.name === "@everyone");
  
  let parent = guild.channels.cache.find(ch => {
    //console.log(ch.id,secret.convoCat.id);
    return ch.id == secret.convoCat.id;
  });
 
  return guild.channels.create(convo.chName, {
    type: "text",
    parent: parent,
    permissionOverwrites: [
      {
        id: everyoneRole.id, //private channel
        deny: ["VIEW_CHANNEL", "SEND_MESSAGES"]
      }, 
      {
        id: client.user.id, //bot permissions
        allow: [  "VIEW_CHANNEL",  "SEND_MESSAGES",  "READ_MESSAGE_HISTORY",  "MANAGE_CHANNELS" ]
      },
      {
        id: user.user.id, //a different user
        allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY"]
      }
    ]
  });
}

1 个答案:

答案 0 :(得分:1)

根据 Discord documentation for the 'Edit Channel Permissions' route(这是 name + each (key + value) pair 最终使用的):

<块引用>

需要 updateOverwrite 权限。只能允许/拒绝您的机器人在公会或频道中拥有的权限(除非您的机器人在频道中有 MANAGE_ROLES 覆盖)。