Search code examples
javascriptnode.jsdiscorddiscord.js

I'm trying to fetch the bot's permissions to check if the command will be able to run


I'm trying to fetch the bot's permissions to check if the command will be able to run. I have the following code:

let botid = "idbot"
let bot = client.users.cache.get(botid)
if (!bot.permissions.has("ADMINISTRATOR")) 
  return message.channel.send("Para mim poder liberar a função de registro eu preciso da permissão **Administrador**, por favor peça para algum **Staff** do servidor configurar, Obrigado.")

I receive the following error:

if (!bot.permissions.has("ADMINISTRATOR"))
                    ^
TypeError: Cannot read properties of undefined (reading 'has')

Solution

  • bot is a User and Users don't have permissions, only GuildMembers do. For this, you'll need to grab the Guild (either by getting by its ID, or just using message.guild). guild#me will be your bot as a GuildMember of that guild.

    You could also just use the fetch method on guild.members. Anyway, you'll need a guild to check your bot's permissions.

    let botid = 'idbot';
    let guild = client.guilds.cache.get('GUILD_ID');
    // OR get it from the message
    let { guild } = message;
    
    let bot = guild.me;
    // OR fetch it from members
    let bot = await guild.members.fetch(botid);
    
    if (!bot.permissions.has('ADMINISTRATOR'))
      return message.channel.send(
        'Para mim poder liberar a função de registro eu preciso da permissão **Administrador**, por favor peça para algum **Staff** do servidor configurar, Obrigado.',
      );