'How to make Discord Bot Automatically Role Someone when they have certain word in status

trying to make it so if someone for example put my discord servers invite in their status ".gg/testing" it would give them a role and if they removed it would take their role away heres the code i have got right now off a similar stack overflow post heres my code right now but it doesnt give the user there role when they have .gg/testing in their status any tips?

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

const roleID = "972823139119685712";

client.on("presenceUpdate", async (_, newPresence) => {
  const role = newPresence.guild.roles.cache.get(roleID);
  const status = ".gg/testing";
  const member = newPresence.member;

  if (member.presence.activities[0].state?.includes(status)) {
    return newPresence.member.roles.add(role);
  } else {
    if (member.roles.cache.has(role)) {
      newPresence.member.roles.remove(role);
    }
  }
});
    
client.login("");


Solution 1:[1]

This is the code you're looking for. Credit to HellCatVN whose code I edited a tiny bit to make this work.

client.on('presenceUpdate', async (oldPresence, newPresence) => {
    const role = newPresence.guild.roles.cache.get("941814488917766145");
    const member = newPresence.member
    const activities = member.presence.activities[0];
  
    if (activities && (activities.state.includes( ".gg/testing" ) || activities.state.includes("discord.gg/testing" ))) {
      return newPresence.member.roles.add(role)
    } else {
      if(member.roles.cache.get(role.id)) {
        newPresence.member.roles.remove(role)
      }
    }
})
The role will be gone if the user turns on invisible status too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamza Waqar