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

203
Visualizações
discord.js cannot read property 'roles' of null

I'm having some trouble with my discord bot and I could really use some help

Currently my bot executes this piece of code, and it works perfectly most of the time:

if (message.member.roles.cache.has('917521104908742736')) //muted role
{
        return message.delete();
}

However, from time to time, the bot crashes randomly, giving out the following error:

TypeError: Cannot read property 'roles' of null

I don't know what to do anymore, and even worse is that the program crashes randomly, so I don't know what exactly causes the error. Could you help me, please?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The message object doesn't seem to have the member property (maybe because the message has been sent in DMs), try to add the interogation mark for Optional chaining :

if(message.member?.roles.cache.has('917521104908742736')) {
  return message.delete();
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe you should try this

if (message.member.roles.cache.some(role => role.id === 'RoleId')) {
 return message.delete();
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The bot simply receives DMs. Check if it's a DM, and return early if it is:

if (!message.guild) return;
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