我已经问了一个与此相关的问题,但是我得到的答案有点瑕疵,直到现在我才注意到。我希望仅当message.author的最高角色高于上述用户的最高角色时,才能使用add role命令。这是我上次查询时得到的答案:
const highest = member.roles.highest; // this is the members highest role
// if role is higher or equal to highest role
if(highest.comparePositionTo(role) <= 0)
return message.channel.send('You cannot add roles equal/higher to that member\'s highest role')
尽管有效,但不是我想要的,该命令仅在角色message.author提到的角色低于提到的用户的最高角色时才有效。所以我将其修改为:
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "addrole",
description: "adds a role to a member",
execute: async (message , args) => {
if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("Invalid Permissions")
let member = message.mentions.members.first();
let role = message.mentions.roles.first();
if (!member)
return message.reply("Please mention a valid member of this server");
if (!role) return message.reply("please mention a valid role of this server");
const highest = message.author.roles.highest;
if(highest.comparePositionTo(role) <= 0)
return message.channel.send(`You cannot add roles equal/higher to that member's highest role`)
let addroleembed = new MessageEmbed()
.setTitle(`:white_check_mark: ${role.name} has been added to ${member.user}`)
.addField(`hippity hoppity this role is now ${member.user}'s property`)
.setFooter(`Action performed by ${message.author.tag}`)
.setColor('77B255')
addroleembed.setTimestamp();
member.roles.add(role);
message.channel.send(addroleembed)
}}
但这似乎不起作用。我收到错误消息“ TypeError:无法读取未定义的属性”最高”。 如果有人可以在这里帮助我,我将不胜感激
答案 0 :(得分:2)
const highest = message.author.roles.highest;
作者是用户,用户没有角色。这就是说它的原因:cannot read prperty of undefined
因为author.roles
不是问题。您必须获得成员才能获得角色。这样做:
const highest = message.member.roles.highest;
在这里,您将邮件的作者(用户)作为GuildMember(成员),然后要求该成员担任最高职务。