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

223
Views
How to add role to a user

I have looked at several guides online and they mostly use this command

let role = message.guild.roles.cache.find(r => r.id === "Role ID");

// The member you want to add the role to
let member = message.mentions.members.first();

// Add role to the member
member.roles.add(role);

// Or add it to yourself
message.author.roles.add(role);

I tried this a lot and this is my implementation of that code

if (message.member.roles.cache.some(role => role.name === "Co-Owner" || role.name === "Operator")) {
    if (command == "birthday") {
      const mention = message.mentions.users.first() || client.users.get(args[0]);
      var role = message.guild.roles.cache.find(role => role.name === "BIRTHDAY POG");

      if (mention.roles.cache.some(role => role.name === "BIRTHDAY POG")) {
        message.reply("The user has already the role, the role will now be removed");
        mention.roles.remove(role);
      }
      else {
        mention.roles.add(role);
        message.reply("The role has been added to `" + mention.username + "`");
        client.guilds.cache.get('731368965963513936').channels.cache.get('778124805135663104').send(message.author.username + " has used the command Birthday to " + mention.username);
        message.reply("Use the same command with the person to remove the role");
      }

    }
  }

This command should add the role of birthday to the mentioned user but I can't make it work, it gives this error

      if (mention.roles.cache.some(role => role.name === "BIRTHDAY POG")) {
                        ^

TypeError: Cannot read property 'cache' of undefined

Can someone help me out?

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

0

Have a look at this https://stackoverflow.com/a/63979077/15107810

But basically, you need to change

message.mentions.users.first() || client.users.get(args[0]);

to

message.mentions.members.first() || message.guild.members.cache.get(args[0])
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!