在控制台中出现 "Missing Access" 错误

Getting "Missing Access" error in console

我正在 discord.js v13 中添加角色命令,这是我收到的错误: Error

const { MessageEmbed } = require("discord.js");
const Discord = require("discord.js")
const config = require("../../botconfig/config.json");
const ee = require("../../botconfig/embed.json");
const settings = require("../../botconfig/settings.json");

module.exports = {
  name: "addrole",
  category: "Utility",
  permissions: ["MANAGE_ROLES"],
  aliases: ["stl"],
  cooldown: 5,
  usage: "addrole <user> <role>",
  description: "Add a role to a member",

  run: async (client, message, args, plusArgs, cmdUser, text, prefix) => {

    /**
     * @param {Message} message
    */

    if (!message.member.permissions.has("MANAGE_ROLES")) return message.channel.send("<a:mark_rejected:975425274487398480> **You are not allowed to use this command. You need `Manage Roles` permission to use the command.**")

    const target = message.mentions.members.first();
    if (!target) return message.channel.send(`<a:mark_rejected:975425274487398480> No member specified`);
    const role = message.mentions.roles.first();
    if (!role) return message.channel.send(`<a:mark_rejected:975425274487398480> No role specified`);

    await target.roles.add(role)
    message.channel.send(`<a:mark_accept:975425276521644102> ${target.user.username} has obtined a role`).catch(err => {
      message.channel.send(`<a:mark_rejected:975425274487398480> I do not have access to this role`)
    })
  }
}

你的错误意味着你的机器人没有权限给用户一个角色。它可能会尝试添加一个比机器人自己的角色地位更高的角色。阻止错误的一种方法是将机器人置于最高位置。该机器人还需要 Discord Developers page to successfully add roles in the first place. If you want to learn more about role permissions, I suggest you go here => Roles and Permissions 中的 MANAGE_ROLES 权限。此外,当您在最后使用 .catch() 时,它所检查的只是最后的 message.channel.send() 是否有效,如果没有,则向频道发送一条消息,告知机器人无法添加角色。相反,您需要在添加角色后使用 .then(),然后使用 .catch() 来捕获错误。那么,您的代码可能如下所示:

target.roles.add(role).then(member => {
    message.channel.send(`<a:mark_accept:975425276521644102> ${target.user.username} has obtined a role`)
}).catch(err => {
    message.channel.send(`<a:mark_rejected:975425274487398480> I do not have access to this role`)
})