0
votes

How can I get the highest role name of a mentioned member? I tried something like this but it doesn't work. Thanks! :) btw this is a ban command and I need this bc my bot is crashing when someone is trying to ban a user with a higher rank than bot.

if(message.member.hasPermission('BAN_MEMBERS')){
    const user = message.mentions.users.first()
    console.log(user.roles.highest.name)
    if(!user) return console.log("test1")
    if(!args[2]) return console.log("test2")
    const ddays = args[1]
1
Can you share more code? For example a sample of your user and message objects?10110
I edited it... ik it doesn't help but I just need a code to check highest rank of usert33o
Both objects mentioned in your comment are introduced by Discord.JS, a wrapper for the Discord APIPascal Stockert
i mean when i try -ban @player , the bot will check if @player has a higher role than the bott33o
@PascalStockert wdym ?t33o

1 Answers

0
votes

What you could do is:

Getting a user's highest role:

  1. Get the UserId from the Mention inside of the Message
  2. Get the Cache from the RoleManager of the Guild in which the Message was sent in
  3. (I don't know if the roles in RoleCache are sorted by position, so sort if needed)
  4. Iterate through the roles in RoleCache and check if the UserId is contained inside a specific role
  5. Get the position of the role

Getting the bot's highest role:

  1. Repeat steps 2-5 for your bot (or integrate them within the previous iteration of the RoleCache)

Then compare both numbers and find out if the bot's "role number" is higher than the one of the user's.