简体   繁体   中英

Node.js Discord.js UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined

I am using discord.js, I was making a ban command and I got this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'calculatedPosition' of undefined

My code is:

let banUser = message.guild.member(message.mentions.users.first())
let bannerHighRole = message.member.highestRole.calculatedPosition;
let getBannedHighRole = banUser.highestRole.calculatedPosition;
if (bannerHighRole < getBannedHighRole) return message.reply("You cannot ban users that have higher roles than you!")
if (bannerHighRole = getBannedHighRole) return message.reply("You cannot ban users that have same highest role!")

How can I fix this error? I'm running the bot on Discord.js v12

This must be v11 code or before, check docs to update your other code, but regardless:

They have changed a lot of the camelCase functions like fetchMessages into things like messages.fetch , might be useful to know.

// you can use message.mentions.members instead of converting a user
let banUser = message.mentions.members.first();
let bannerHighRole = message.member.roles.highest.position;
let getBannedHighRole = banUser.roles.highest.position;
if (bannerHighRole < getBannedHighRole) return message.reply("You cannot ban users that have higher roles than you!");
//you had x = y, which is an assignment
if (bannerHighRole === getBannedHighRole) return message.reply("You cannot ban users that have same highest role!");

Also saw the full code and you should prob check if the user has mod/admin before you calculate all of these.

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