How to assign roles with a command – discord.js

I wanted to give my bot the functionality to assign roles with a command

For example, +mod @user would give @user the role of Mod.

Code in my main.js:

if(command == 'mod'){
    client.commands.get('mod').execute(message, args);

Code in my mod.js file:

module.exports = {
    name: 'mod',
    description: "This command gives member the Mod role",
    execute(message, args){
        const member = message.mentions.users.first();
        member.roles.add('role ID xxxx');

I get an error saying the member is empty. Am I doing something wrong?

Leave a Comment