Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
To check if a mentioned user has the role or not in discord.js

I am making a administration bot in discord with discord.js. I am trying to make a command which gives the user a specific role. So I want to make it check first if that user has that specific role beforehand or not to avoid errors. The problem lies in the if statement which checks if the user has the role, its giving me a error "TypeError: Cannot read properties of undefined (reading 'has')". Please suggest me how to fix this.

const {
    Role,
    ThreadManager
} = require("discord.js");
const ms = require('ms');

module.exports = {
    name: 'gulag',
    description: "gulag",
    execute(message, args) {

        if (message.member.permissions.has("ADMINISTRATORS")) {
            const target = message.mentions.users.first();
            const check = message.mentions.users.first();
            if (target) {

                let gulagRole = message.guild.roles.cache.find(role => role.name === 'gulag');
                let memberTarget = message.guild.members.cache.get(target.id);


                if (check.roles.has(gulagRole.id)) {
                    message.reply(`<@${memberTarget.user.id}> is already in gulag.`)
                } else {
                    memberTarget.roles.add(gulagRole.id);
                    message.channel.send(`<@${memberTarget.user.id}>`);
                    message.channel.send('https://tenor.com/view/gulag-warzone-call-of-duty-cod-battle-royale-gif-16853559');
                }


            }

        }

    }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would go straight for message.mentions.members.first() because this will return GuildMember so it is easier to then check the roles

const target = message.mentions.members.first()
if(target){
      if(target.roles.cache.find(r => r.name === 'gulag')){
            //REST OF THE STUFF
      }
}

If you are using v13 remember to declare Intents for your Client as well.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!