Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda