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:

bot.on("presenceUpdate", (oldMember, newMember) => {
    let guild = newMember.guild;
    let playRole = guild.roles.get("633589794357641235");
    if (!playRole) return;

    if (newMember.user.presence.game && newMember.user.presence.game.name === "Golf With Your Friends") {
        newMember.addRole(playRole);
    }
});

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:

client.on("presenceUpdate", (oldPresence, newPresence) => {
  //....
}

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

const playRole = guild.roles.cache.get("633589794357641235");

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:

if (newPresence.find(e => e.name === "Golf With Your Friends")) {
  //...
}

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

newPresence.member.roles.add(playRole);

Full code:

bot.on("presenceUpdate", (oldPresence, newPresence) => {
    let guild = oldPresence.guild;
    //guild.roles => guild.roles.cache
    let playRole = guild.roles.cache.get("633589794357641235");
    if (!playRole) return;

    if (newPresence.find(e => e.name === "Golf With Your Friends")) {
        newPresence.member.roles.add(playRole);
    }
});
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement