我正在使用presenceUpdate
事件,但是它触发了两次,并被告知它正在释放与该机器人具有的共享服务器数量。目前,我的代码两次输出online
。我不确定如何只输出一次。
if (newPresence.userID === botid) {
if (newPresence.status === 'online') {
console.log(newPresence.status); // Should output 'online' currently outputs 'online online'
答案 0 :(得分:0)
根据discord.js' docs presenceUpdate
是Emitted whenever a guild member's presence (e.g. status, activity) is changed.
因此,与机器人拥有多少共享服务器无关。
您可以每5分钟尝试使用Presence#status
检查客户端的ping,您可以编写如下函数:
function checkStatus() {
if (client.users.fetch(botID).presence.status === "online" ) {
return true;
} else {
return false;
}
}
然后您可以运行以下代码:
client.setInterval(() => checkPing(), 300000);
300000
是以毫秒为单位的5分钟,因此每5分钟就会运行一次该功能。
您可以阅读有关client.setInterval()
答案 1 :(得分:0)
我只是通过比较状态来做到的。
if (oldPresence.status === newPresence.status) return;
答案 2 :(得分:0)
我注意到这个问题仍未解决,所以我会为您解决。 所以上面的答案是正确的,但是,它们都没有说明机器人为此需要的意图。
您看,除非您启用了在线权限网关意图,否则 presenceUpdate
不会发出。对于少于 100 个服务器的机器人,可以通过拨动开关在开发人员门户上切换。否则,您需要为不和谐打开工单以启用在线意图。
启用意图后,试试这个:
client.on(`presenceUpdate`,(member)=>{
console.log(`${member.user.username} has updated their presence.`)
})
然后尝试将您的状态更改为 dnd。如果您启用它,您应该看到
[your tag] has updated their presence.
。否则你可能做错了什么。