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')
CodePudding user response:
Because bot
as you defined it is just a User
, meaning it just has informations like it's tag, username, servers it is in etc.. what you want is a GuildMember
, it's the same person but on a specific server, it has roles, a hoist color and permissions
which is what you're searching for :
let bot = message.guild.me; // The bot's GuildMember object
if (!bot.permissions.has("ADMINISTRATOR")) return message.reply('I am not an admin!')
CodePudding user response:
bot
is a User
and User
s don't have permissions
, only GuildMember
s 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.',
);