Skip to content
Advertisement

Getting “Missing Access” error in console

I’m working on an add role command in discord.js v13 and this is the error I am getting: 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`)
    })
  }
}

Advertisement

Answer

What your error means is that your bot doesn’t have permissions to give a role to a user. It might be trying to add a role which has a higher position than the bot’s own role. One way to stop the error is to give the bot the highest position. The bot will also require the MANAGE_ROLES permission in the 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. Also, when you are using .catch() at the end, all it is checking for is if the message.channel.send() at the end worked and if not, then send a message to channel telling that the bot was not able to add the role. Instead you need to use .then() after adding the role and then use .catch() to catch the error. Then, your code might look like this:

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`)
})
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement