简体   繁体   English

discord.js v13 permissions.has() function 不工作(TypeError: Cannot read properties of undefined (reading 'has'))

[英]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.permissionsundefined It's because your mention variable is a User and only GuildMember s have permissions .这是因为您mention的变量是User并且只有GuildMemberpermissions

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.

相关问题 无法读取未定义的属性(读取“权限”)。 | Discord.js v13 - Cannot read properties of undefined (reading 'permissions'). | Discord.js v13 类型错误:无法读取未定义的属性(读取“设置”)discord.js v13 斜杠命令处理程序 - TypeError: Cannot read properties of undefined (reading 'set') discord.js v13 slash commands handler TypeError:无法在 discord.js v13 中读取未定义的属性(读取“路径”) - TypeError: Cannot read properties of undefined (reading 'path') in discord.js v13 discord.js v13 - 类型错误:无法读取未定义的属性(读取“正常运行时间”) - discord.js v13 - TypeError: Cannot read properties of undefined (reading 'uptime') DISCORD.JS V13 TypeError:无法读取未定义的属性“具有” - DISCORD.JS V13 TypeError: Cannot read property 'has' of undefined Discord.js v13 无法读取属性 o 未定义的读取发送 - Discord.js v13 cannot read properties o undefined reading send 无法读取未定义的属性(读取“发送”)(Discord.js v13) - Cannot read properties of undefined (reading 'send') (Discord.js v13) Discord.js:TypeError:无法读取未定义的属性(读取“has”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'has') Ping 为负值 (Discord.js v13) - Ping has a negative value (Discord.js v13) 类型错误:无法读取 null discord.js v13 的属性“setPresence” - TypeError: Cannot read property 'setPresence' of null discord.js v13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM