Skip to content
Advertisement

Discord.js: Add role to message sender

i am trying to make a discord.js bot that adds a role to a user when they type: +rolename .

This is what I have come up with:

JavaScript

When running the code i get the following error:

TypeError: Cannot read property ‘add’ of undefined

This doesn’t happen when I use this code and type +test @DiscordName#0001:

JavaScript

My question is: How can I add the role to the message author.

Thanks in advance

Advertisement

Answer

The problem is that your authoruser is the users id (= string) not the member. You cannot add roles to users. Also if you get the role’s id and not the name of the role you can add the role with the role’s id.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement