我正在制作一个set昵称命令,下面是我的代码:
if (message.mentions.members.first().roles.highest.position > bot.user.roles.highest.position) return message.channel.send("My highest role is lower than the mentioned user's role");
但那个代码有一个错误:Type error: Cannot read property of "highest" of undefined
我也试过这个:
if (message.mentions.members.first().roles.highest.comparePositionTo(bot.user.roles.highest) > 0) {
return message.channel.send("Your highest role is lower than the mentioned user's role");
}
知道怎么修吗?
bot.user
是一个User
对象,它不包含任何与服务器相关的数据。您要查找的是一个GuildMember
对象。
为了获得你的机器人角色,你需要从公会中获取成员。这可以通过GuildMemberManager.resolve
来实现。你可以使用公会的内置属性。
您得到的代码应该是这样的:
if(message.mentions.members.first().roles.highest.position > message.guild.members.resolve(bot.user).roles.highest.position)
return message.channel.send("My highest role is lower than the mentioned user's role");
有时,.resolve
会返回undefined
。在这种情况下,您需要使用.fetch
。然而,既然你得到了自己机器人的角色,这应该不是什么大问题。