Search code examples
node.jsdiscorddiscord.jsstatus

discord.js auto role from status activity


I'm coding a bot for my server and I want the bot to read the status of all members and give them a particular role if they have the vanity link. I've made this code from scratch and it seems to not work. Here is the code :

client.on('presenceUpdate', async (oldPresence, newPresence) => {
  const role = newPresence.guild.roles.cache.get("844047538797281310");
  const member = newPresence.member


  if (member.user.presence.activities[0]?state.includes( ".gg/moonxile" || "discord.gg/moonxile" )) {
    return newPresence.member.roles.add(role)
  } else {
    if(member.roles.cache.has(role.id)) {
      newPresence.member.roles.remove(role)
    }
  }
})

I'm sort of new to coding and I'm not able to tell where I've gone wrong


Solution

  • At first .includes js method is not support condition in it And member.user.presence.activities is array of activity and states is it's property so the code should be look like

    client.on('presenceUpdate', async (oldPresence, newPresence) => {
        const role = newPresence.guild.roles.cache.get("844047538797281310");
        const member = newPresence.member
        const activities = member.user.presence.activities[0];
      
        if (activities && (activities.state.includes( ".gg/moonxile" ) || activities.state.includes("discord.gg/moonxile" ))) {
          return newPresence.member.roles.add(role)
        } else {
          if(member.roles.cache.get(role.id)) {
            newPresence.member.roles.remove(role)
          }
        }
    })