0
votes

I have asked a question somewhat related to this already, but the answer I got was slightly flawed, and I didn't notice until now. I want the add role command to work only when the message.author's highest role is above the mentioned user's highest role. This was the answer I got from the last time I asked a query:

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')

although it worked, it was not what I was looking for, this command will only work when the role message.author mentioned is below the mentioned user's highest role. So I modified it to this:

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)

}}  

but this does not seem to work. I get the error "TypeError: cannot read propert 'highest' of undefined. I would appreciate it if someone could help me out here

1
No, the issue was I was confused between message.author and message.memberpeter 23
message.author returns a User and message.member is a GuildMember. It's the same issue.Lioness100
oh, I didnt know that, sorrypeter 23

1 Answers

2
votes

const highest = message.author.roles.highest;

author of a message is a user and users don't have roles. That is the reason it's saying: cannot read prperty of undefined because author.roles is not a thing. You have to get the member, to get the roles. So do this:

const highest = message.member.roles.highest;

Here you are taking the author(user) of the message as a GuildMember(member) and then asking for the highest roles this member have.