【问题标题】:Discord.js: How read users status with a bot on server?Discord.js:如何使用服务器上的机器人读取用户状态?
【发布时间】:2017-09-03 14:07:51
【问题描述】:

我写了一个小机器人,需要服务器上所有用户的状态(在线、离线、空闲)。我找到了https://discord.js.org/#/docs/main/master/class/Client?scrollTo=users

bot.on('ready', function() { console.log(bot.users); });

这显示所有用户:

'352....128': User { username: 'NAME', id: '352....128', discriminator: '5000' avatar: null, bot: false },

所以没有状态。 Bot 无权访问这个?在 embed.js 中我可以读取所有在线和空闲用户的状态(离线用户未列出):https://discordapp.com/api/guilds/SERVERID/embed.json

{"username": "NAME", "status": "online", "nick": "NICK", "avatar_url": "...", "avatar": "...", "discriminator": "5000", "id": "..."}

embed.json 是获取所有在线和空闲用户状态的唯一方法吗?所以我需要安装这个包吗? https://www.npmjs.com/package/jsonfile 并阅读此文件?

【问题讨论】:

  • 如果有效请告诉我

标签: node.js


【解决方案1】:

你可以在这里找到它:https://discord.js.org/#/docs/main/stable/typedef/PresenceStatus

您可以使用:

user.presence.status

或:

member.user.presence.status

它返回“online”、“idle”、“dnd”或“offline”

【讨论】:

    【解决方案2】:

    甚至更简单:

    .addField('Status', message.author.presence.status)
    

    【讨论】:

      【解决方案3】:

      你需要让你的机器人自己设置状态,例如

      const Discord = require("discord.js");
      const client = new Discord.Client();
      
      client.on('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
        client.user.setStatus("online");
      });        
      
      
      client.on("disconnected", () => {
          client.user.setStatus("offline");
          console.log(client.user); //returns the current user
      });
      
      console.log(client.users); //returns all users
      

      要查看所有客户端事件,请参阅here

      更新

      client.on("message", function(msg){
          if(client.user.isAdmin){ //checking as an admin for example
            if(msg.content == "-showUsers"){
               client.deleteMessage(msg, callBackFunc);
               console.log(bot.users);
            }
          }
      })
      

      【讨论】:

      • 谢谢!已经在服务器上的用户怎么办?您只捕获新的连接和断开连接,“空闲”状态是什么? :)
      • 我做了一小段代码作为更新,我不知道它是否有效,但它基本上是在频道中输入一个命令来检索所有用户:) 请告诉我如果它有效。我不太确定。 @user706420
      【解决方案4】:
      var KlausAlge = "Klaus Alge"
      
      for (KlausAlge in map) return true
      for (!KlausAlge in map) return false
      

      【讨论】:

      • 您能否解释一下为什么您的答案是一个好的解决方案以及它是如何按照stackoverflow.com/help/how-to-answer工作的?
      • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案以添加解释并说明适用的限制和假设。 From Review
      猜你喜欢
      • 2022-01-07
      • 2018-09-24
      • 2021-10-09
      • 2022-11-11
      • 2021-01-11
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2020-12-10
      相关资源
      最近更新 更多