Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda