Search code examples
discord.jsroles

Discord.js v12 Role add to mentioned user problem


so I am trying to make a command like $roletested @user which should give the user mentioned the specific role. I get an error called:_ "Cannot read property 'roles' of undefined... Help me out please, here's my code :D

    if (message.content.startsWith(prefix + "roletested")) {
        let testedUser = message.mentions.members.first()
        if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'));
        if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌');
            declineMsg.delete({timeout: 5000});
            let testedRole = message.guild.roles.cache.find(role => role.id === "724676382776492113");
            testedUser.roles.add(testedRole);
            message.channel.send("Added the role `TESTED` to user.")
        })}})

Solution

  • Your code but fixed

      if (message.content.startsWith(prefix + "roletested")) {
        if (!message.member.hasPermission('MANAGE_ROLES')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))
    
        let testedRole = message.guild.roles.cache.get('724676382776492113');
        let testedUser = message.mentions.members.first();
        if(!testedUser) return message.channel.send("You have to mention the person you want to assign the role to!").then((declineMsg) => { message.react('❌')
        declineMsg.delete({timeout: 5000});
        });
        
        testedUser.roles.add(testedRole);
        message.channel.send("Added the role `TESTED` to user.")
      }