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

143
Views
Discord.js how to give role to a pinged user

When I am trying to give a role to a pinged member, it throws an error;

member.guild.roles.add is not a function

My current code:

const member = message.mentions.members.first(); //gets pinged user in the message
var role = message.guild.roles.cache.get('role-id') //finds role
if (!role) return message.reply("Can't find the role")
member.guild.roles.add(role); //gives role

I'm pretty sure it's an error with using message.mentions.members.first(), but how can I use message.mentions.members.first() (similarly like message.author) to give the user roles etc?

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

0

You are getting this error simply because you are trying to add your role to the guild instead of the user who was pinged. So all you have to do is change your code to:

if (!message.mentions.members.first()) return message.reply('Please mention a user')
const member = message.mentions.members.first();

var role = message.guild.roles.cache.get('role-id')
if (!role) return message.reply("Can't find the role")
member.roles.add(role);
about 4 years ago · Juan Pablo Isaza Report

0

You can use double mention to make this command,

const member = message.mentions.members.first() || await message.guild.members.fetch(args[0]) || await message.guild.members.cache.get(args[0]) //The two await is for your members ID
const role = message.mentions.members.first(1)[2] || await message.guild.roles.fetch(args[1]) || await message.guild.roles.fetch(args[1]) //The (1)[2] will read as args[2] == 2 which is the your second input.

To prevent any errors, you need to return if 1 of this 2 return as a null,

if(!member) return message.reply("your_reply")
if(!role) return message.reply("your_reply")

Then add the role now

member.roles.add(role)
//Then your message if its successfully added.
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!