简体   繁体   English

“user.setStatus”不会更新机器人的状态 Discord.JS

[英]“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 ).我希望当游戏服务器被检测为离线时,状态变为橙色( idle ),当服务器在线时,状态变为绿色( 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.为此,我使用client.user.setStatus('online')client.user.setStatus('idle')但状态没有改变,只有消息被更新。

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...只有消息被更新,而不是状态......

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:您还可以使用setPresence通过单个命令更新状态和活动:

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM