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

216
Views
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 answers
Answer question

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 Report

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 Report

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 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!