0

no clue why its not working, can somebody help me? I despair.

bot.on("message", (message) => {
    var roles = message.author.roles
    let roleID = '1234567890';
    if (message.author.bot) return; 
    if(message.content.startsWith(prefix + 'ttt')) {
        roles.add(roleID);          
    }
});

The Error is the same like in the title.
        roles.add(roleID);
              ^

TypeError: Cannot read property 'add' of undefined  

emirate.
  • 11
  • 7

3 Answers3

0

There is no property roles of message.author. You may want to look at message.member instead, which has that property.

Relevant Links:

BattleMage_
  • 76
  • 1
  • 3
0

If you want to add the role to the person who used it, you should get the message member and give it.

bot.on("message", (message) => {
    var roles = message.author.roles
    let roleID = '779756792737300490';
    if (message.author.bot) return; 
    if(message.content.startsWith(prefix + 'ttt')) {
        message.member.roles.add(roleID);          
    }
});

I just changed the 6th line.

You also can't give a role to a user. You can only give it to a member.

You can also take a look at docs:

0

This will work:

bot.on("message", (message) => {
    var guildMember = message.member
    let roleID = '779756792737300490';
    let roleNAME = 'TestRole';
    let userID = message.author.tag + " " + (message.author.id);
    if (message.author.bot) return; 
    if(message.content.startsWith(prefix + 'ttt')) {
        guildMember.roles.add(roleID); 
        console.log(`Give "${roleNAME}" to User "${userID}" `)    
    }
    if(message.content.startsWith(prefix + "zzz")) {
        guildMember.roles.remove(roleID);
        console.log(`Removed "${roleNAME}" from User "${userID}"`)
    }
});

I also put a console.log code in it, so you can see if somebody removed a role or gave a role, it will also work if you remove the 2 lines of Code ;)

emirate.
  • 11
  • 7