【问题标题】:I am making a bot hat gives a role to a user but it get's error that the bot doesn't have permissions我正在制作一个机器人帽子给用户一个角色,但它得到的错误是机器人没有权限
【发布时间】:2022-02-17 02:54:01
【问题描述】:

(代码)

const Command = require("../Structures/Command.js");

module.exports = new Command({
    name: "give-role",
    description: "gives you the role",
    permission: "ADMINISTRATOR",
    async run(message, arguments, client) {
        const targetUser = message.mentions.users.first();
        if (!targetUser) {
            message.reply("Tag the user that you want to give the role to");
            return;
        }

        arguments.shift();

        const roleName = arguments[1];
        const { guild } = message;

        const role = guild.roles.cache.find((role) => {
            return role.name === roleName;
        });
        if (!role) {
            message.reply(`There is no role called: ${roleName}`);
            return;
        }

        const member = guild.members.cache.get(targetUser.id);
        member.roles.add(role);

        message.reply(`That user now has the role : ${roleName}`);
    }
});

(错误信息)

/home/runner/Discord-manager/node_modules/discord.js/src/rest/RequestHandler.js:350
      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Missing Permissions
    at RequestHandler.execute (/home/runner/Discord-manager/node_modules/discord.js/src/rest/RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (/home/runner/Discord-manager/node_modules/discord.js/src/rest/RequestHandler.js:51:14)
    at async GuildMemberRoleManager.add (/home/runner/Discord-manager/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:124:7) {
  method: 'put',
  path: '/guilds/911548130917498901/members/854133415364263976/roles/924275494810165259',
  code: 50013,
  httpStatus: 403,
  requestData: { json: undefined, files: [] }
}

错误是 DiscordAPIError: 50013,错误是因为提示说机器人没有权限,但我添加了管理员,但它仍然说它没有权限,我不知道为什么。请帮助我(对不起,如果我的英语不好:())

【问题讨论】:

  • 你是在服务器(和普通用户一样)还是在开发者门户中授予权限?
  • 在角色方面添加ADMINISTRATOR 权限并不总是足够的,您需要确保机器人的最高角色高于您尝试添加的角色。

标签: discord.js


【解决方案1】:

机器人可能试图赋予比你的机器人更高的角色。要解决此问题,请将机器人位置最高的角色拖到您尝试分配的角色上。

信用: Reddit Comment

【讨论】:

    【解决方案2】:

    在尝试将其添加到用户之前,您需要检查角色的位置,例如:

    if (message.guild.me.roles.highest.position <= role.position) {
        message.reply(`${role.name} is higher than or equal to my highest role.`);
        return;
    }
    

    【讨论】:

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