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

204
Views
TypeError: Cannot read property 'add' of undefined Discord.js simple code

So, I am making a bot and here's a part of my code what has a problem:

    if(message.content.startsWith("$sudo make-mod ")) {
    target = message.mentions.users.first();
    let role = message.guild.roles.cache.find(r => r.id = "<@&938235657929773077>")

    console.log(target)
    target.roles.add(role.id)
}

and I get this error:

    target.roles.add(role.id)
                     ^

TypeError: Cannot read property 'add' of undefined

I don't know what's the problem. Can someone help???

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When you use message.mentions.users.first(), it returns a User which doesn't have a roles property. Instead, you need a GuildMember, which does have the roles property and you can fetch the GuildMember by using message.mentions.members.first(). You can also add an if check to see whether the mentioned user was valid or not by using this: if (!target) return

about 4 years ago · Juan Pablo Isaza Report

0

You need to use members

target = message.mentions.members.first()
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!