【问题标题】:How can i check if a person has went online, offline, etc. in discord.js?我如何在 discord.js 中检查一个人是否上线、下线等?
【发布时间】:2020-09-06 21:29:16
【问题描述】:
const channel = client.channels.cache.get('<channelid>');
const person1 = client.users.cache.get('<userid>');
const person = client.users.cache.get('<userid>');

client.on('message', message =>{
  client.on('presenceUpdate', () =>{

    if(person1.user.presence.status === 'dnd' || person1.user.presence.status === 'online'){
      channelforstatus.send('person1 is now online');
    }

    else if(peron1.user.presence.status === 'offline' || person1.user.presence.status === 'idle'){
      channel.send('person1 is offline');
    }

client.on('message', message => {
  client.on('presenceUpdate', () =>{

    if(person.user.presence.status === 'dnd' || person.user.presence.status === 'online'){
      channel.send('person is now on');
    }

    else if(person.user.presence.status === 'offline' || person.user.presence.status === 'idle'){
      channel.send('person is now off');
    }

  });
});

  });

});

这是我尝试过的,.send() 该功能不起作用。我到处寻找,没有发现任何可以帮助我解决这个问题的东西。我只需要它,因此它每次都会检查特定人员是否上线、下线等。并向特定频道发送消息。

【问题讨论】:

  • 顺便欢迎来到 Stackoverflow!别忘了看看tour ????

标签: node.js discord.js


【解决方案1】:

如果您使用状态但离线而不是用户在线,我花了一整天的时间寻找答案,所以无论如何都会分享它z

presence.status 的常见错误是忘记在开发人员应用程序中检查这些内容。我不知道是什么意思

A screenshot

现在在你的消息(命令处理程序)函数上.. 如果你有一个

message.guild.members.cache.get('userId').presence.status

${message.author.username} is now ${message.author.presence.status};

如果我知道如何显示所有用户而不是一个用户,我会更新此内容

我的第一篇文章...我会记住这个 xD

【讨论】:

  • 非常感谢!我很生气,因为机器人不断显示我的状态为离线,我只是启用了屏幕截图中显示的那些选项,它就开始工作了。
【解决方案2】:

首先,要遵守的一条规则是事件侦听器应该始终位于代码的顶层,并且永远不要嵌套。 (否则您会受到内存泄漏和其他问题的影响,例如重复和意外的代码执行)。

client.on("message", (message) => {
    ...
});

client.on('presenceUpdate', (oldPresence, newPresence) => {
    ...
});

现在查看presenceUpdate 事件和Presence 对象文档时,您可以设法查看状态是否像这样演变:

client.on('presenceUpdate', (oldPresence, newPresence) => {
    let member = newPresence.member;
    // User id of the user you're tracking status.
    if (member.id === '<userId>') {
        if (oldPresence.status !== newPresence.status) {
            // Your specific channel to send a message in.
            let channel = member.guild.channels.cache.get('<channelId>');
            // You can also use member.guild.channels.resolve('<channelId>');

            let text = "";

            if (newPresence.status === "online") {
                text = "Our special member is online!";
            } else if (newPresence.status === "offline") {
                text = "Oh no! Our special member is offline.";
            }
            // etc...

            channel.send(text);
        }
    }
});

请注意presenceUpdate 事件由用户和机器人共享的每个公会触发,这意味着如果用户状态更改并与您的机器人共享两个公会,则此代码将执行两次。

【讨论】:

  • 我可以让它发送一个特定的消息吗?示例:会员上线:我们的特约会员上线。会员下线:哦不!我们的特殊会员离线。其余状态也是如此。
  • 您需要修改的只是channel.send() 中的字符串,例如channel.send("Our special member is online"); 您可以做任何您想做的事情^^ 让我编辑我的答案来给您一些建议
  • 谢谢!对此,我真的非常感激!这是我第一次来这里,社区非常支持和友善!再次感谢您!
【解决方案3】:

要获取出席信息,你可以使用user.presence,它会获取关于用户的各种信息,但你只需要user.presence.clientStatus.desktop

例如,您的代码将是

bot.on('presenceUpdate', () =>{
    let person1 = bot.users.cache.get('USERID')
    console.log(person1.presence.clientStatus.desktop)
    if(person1.presence.clientStatus.desktop === 'dnd' || person1.presence.clientStatus.desktop === 'online'){
        channel.send('person1 is now online');
    }

    else if(person1.presence.clientStatus.desktop === 'offline' || person1.presence.clientStatus.desktop === 'idle'){
        channel.send('person1 is offline');
    }
})

【讨论】:

  • 我在您的回答中看到了一些问题:除非您真的想针对特定平台,否则仅查看桌面状态可能是一个缺陷,我建议改用 presence.status 属性。在这里,您不会检查状态是否发生变化,这意味着如果用户从idle 转到online,则会发送一条消息。但是,例如,如果用户现在正在玩游戏,它的状态会更新,因此会发送另一条消息,即使他的 status 没有改变。
猜你喜欢
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 2016-07-02
相关资源
最近更新 更多