ThetaHacker
ThetaHacker

Reputation: 25

(discord.js) add role to author

I am looking for help, my code handles a "message" event, and I am trying to make it add a role to the author of the comment

My current attempt is

client.on('message', msg => {msg.member.addRole("[role id]").catch(function(){});})

however, it does not seem to be working for the various attempts I have made. Any help on this?

Thank you!

Upvotes: 1

Views: 2444

Answers (1)

Cipher
Cipher

Reputation: 2722

Use this code. Fist check if user don`t have this role, then give him this role

client.on('message', msg => {
    if (!msg.member.roles.some(role => role.id === 'YourROLEID')) {
            msg.member.addRole('YourROLEID')
            .then(console.log(`Succesfuly added role to member ${msg.author.tag}`))
            .catch(console.error)
    }
})

Upvotes: 2

Related Questions