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?
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);
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.