检查用户和机器人角色是否高于提到的用户(discord.js)



我正在制作一个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。然而,既然你得到了自己机器人的角色,这应该不是什么大问题。

最新更新