简体   繁体   中英

TypeError: message.guild.member is not a function when trying to ban user

I am using the discord.js v12 library for my Discord bot, and overnight when I want to ban a user I got this error:

TypeError: message.guild.member is not a function

In the constants.js file I have included all my commands with different parameters. I also have another message.js file which looks at it if a command is typed and before executing it checks according to the parameters of the command if it is good or not, so I have for my ban.js command that if the user mentioned to the permission BAN_MEMBERS then we do not use the command on them.

Here is my code for this check and even after checking everything well on the discord.js documentation, I don't understand why it gives me an error:

if (command.help.isUserAdmin && message.guild.member(user).hasPermission('BAN_MEMBERS')) {
    return message.reply("you cannot use this command on this user.");
}

member is not a property of the guild object therefore it is returning undefined . I assume you are trying to get the member who sent the message, so your code snippet can be written as:

if (command.help.isUserAdmin && message.member.hasPermission('BAN_MEMBERS')) {
    return message.reply("you cannot use this command on this user.");
}

You can read more about using the Discord API here .

I finally managed to fix the problem:

message.guild.members.cache.get(user.id).ban({days: 0, reason: reason})

I learned that users' collections are mapped by their ids, so just grab and ban the mentioned user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM