Search code examples
javascriptnode.jsdiscorddiscord.jsgamedig

"user.setStatus" doesn't update bot's status Discord.JS


I would like that when the game server is detected as offline, the status becomes orange (idle), and when the server is online, the status becomes green (online).

For this, I used client.user.setStatus('online') and client.user.setStatus('idle') but the status does not change, only the message is updated.

Here is my code:

client.on('ready', () => {
  console.log('OK')

  setInterval(() => {
    Gamedig.query({
      type: 'garrysmod',
      host: 'xx.xx.xx.xx',
      port: '27015'
    })
      .then((updatedState) => {
        state = updatedState;

        const nb_joueursmax = state.maxplayers;
        const nb_joueurs = state.players.length;
        
        client.user.setStatus('online');
        client.user.setActivity(`${nb_joueurs}/${nb_joueursmax} Joueurs`);  
      })
      .catch((e) => {
        console.log(e);
        client.user.setStatus('idle');
        client.user.setActivity('Serveur Hors Ligne');
      });
  }, 6000);
});

I tried this, but it doesn't work. Only the message is updated, not the status...


Solution

  • There doesn't seem to be an error and it should work fine, it probably is just slow to react/update. You could also use setPresence to update the status and the activity with a single command:

    client.on('ready', () => {
      console.log('OK');
    
      setInterval(() => {
        Gamedig.query({
          type: 'garrysmod',
          host: 'xx.xx.xx.xx',
          port: '27015',
        })
          .then((updatedState) => {
            state = updatedState;
    
            const nb_joueursmax = state.maxplayers;
            const nb_joueurs = state.players.length;
    
            client.user.setPresence({
              activity: { name: `${nb_joueurs}/${nb_joueursmax} Joueurs` },
              status: 'online',
            });
          })
          .catch((e) => {
            console.log(e);
            client.user.setPresence({
              activity: { name: 'Serveur Hors Ligne' },
              status: 'idle',
            });
          });
      }, 6000);
    });