I want to program a ban
command for my Discord bot. I have this line where the bot should check if a user has the Administrator permission. If the user that should get banned has them, the bot doesn't ban the user and crashes. When I try to run this command I get this:
TypeError: Cannot read properties of undefined (reading 'has')
I don't understand why. Noone I asked could help me, I found nothing online, so I hope I can find here help.
My Code:
const discord = require('discord.js');
const { Permissions } = require('discord.js');
module.exports.run = async (Client, message, args) => {
if (!message.member.roles.cache.some(role => role.id == 589850931785498624)) {
return message.reply("You don't have the perms.");
}
const mention = message.mentions.users.first();
if (!mention) {
return message.reply('You need to tag a user!');
}
if (mention.permissions.has(Permissions.FLAGS.MANAGE_CHANNELS)) {
return message.reply("You can't ban an Administrator!")
}
//message.guild.members.ban(mention);
}
module.exports.help = {
name: "ban",
aliases: ["b"],
}
"TypeError: Cannot read properties of undefined (reading 'has')" means that mention.permissions
is undefined
. It's because your mention
variable is a User
and only GuildMember
s have permissions
.
Another error is that you try to check if the role.id
is equal to a number/integer but snowflakes (like 589850931785498624
) should always be strings as these are greater than the MAX_SAFE_INTEGER
.
module.exports.run = async (Client, message, args) => {
if (!message.member.roles.cache.some((role) => role.id == '589850931785498624'))
return message.reply("You don't have the perms.");
const mentionedMember = message.mentions.members.first();
if (!mentionedMember)
return message.reply('You need to tag a user!');
if (mentionedMember.permissions.has(Permissions.FLAGS.MANAGE_CHANNELS))
return message.reply("You can't ban an Administrator!");
message.guild.members.ban(mentionedMember);
};