在discord.js 中使用“presenceUpdate”事件时,当状态从“无”更改为“流”时,该事件已成功触发。然而,似乎如果我在触发事件后打开移动客户端,它会再次发生,并且具有完全相同的“oldUser”和“newUser”。我正在使用 VSCode 并在本地运行我的 node.js 实例,因此我可以使用断点进行调试。
client.on("presenceUpdate", (oldUser, newUser) => {
//Exclude title changes and typing
if(oldUser.presence.game != null && oldUser.presence.game.streaming) {
return;
}
if(oldUser.displayName === "Test" && newUser.presence != null){
if(newUser.presence.game != null && newUser.presence.game.streaming){
client.channels.get("channelId").send("@everyone Test has started streaming!");
}
}
});
如您所见,我已尝试使用第一个 if block 来过滤此“双触发”。这是不成功的,因为看起来第一次触发时,新老用户和第二次触发时是一模一样的。我能看出的唯一区别是“clientStatus”现在包含“mobile: online”作为属性。有没有办法从代码 Angular 首先防止这种双重触发,或者我需要对 if 语句进行更好的过滤器?
最佳答案
参见Presence.clientStatus
;它将返回存在适用于什么类型的设备。
使用此属性,您可以将旧用户的客户端与新用户的客户端进行比较,如果发生更改则返回。
if (oldUser.presence.clientStatus !== newUser.presence.clientStatus) return;
关于javascript - 从 "presenceUpdate"事件中排除移动更新吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57794090/