[英]discord.js v13 permissions.has() function not working (TypeError: Cannot read properties of undefined (reading 'has'))
I want to program a ban
command for my Discord bot.我想为我的 Discord 机器人编写一个
ban
命令。 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
. "TypeError: Cannot read properties of undefined (reading 'has')"表示
mention.permissions
是undefined
。 It's because your mention
variable is a User
and only GuildMember
s have permissions
.这是因为您
mention
的变量是User
并且只有GuildMember
有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
.另一个错误是您尝试检查
role.id
是否等于数字/整数,但雪花(如589850931785498624
)应该始终是字符串,因为它们大于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);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.