Skip to content
Advertisement

How to auto-give role when someone is playing game? V12 discord js

I recently decided to upgrade my discord js library to v12. I wanted to when someone turns on the game (Golf With Your Friends) he gets a role. But I can’t program it in discord js v12. Can you help me? Here is my code in v11:

JavaScript

Advertisement

Answer

Here’s a guide from discordjs.guide on updating from v11 to v12:

https://discordjs.guide/additional-info/changes-in-v12.html#managers-cache

You can also check the official docs here:

https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-presenceUpdate

Firstly they updated the parameters to oldPresence and newPresence,

So:

JavaScript

Second they changed the collection from guild.roles to guild.roles.cache:

JavaScript

Third your if statement uses newMember.user.presence which now is just newPresence, game has been modified into an Activity class, and there is no more <Presence>.game, so you will have to loop over the <Presence>.activites array:

JavaScript

And lastly they moved <GuildMember>.addRole to <GuildMember>.roles.add:

JavaScript

Full code:

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