rootProject.name = 'reports-be'
include 'common'
include 'builder'
include 'auth'
include 'builder:base'
include 'builder:reporting'
这是我尝试过的,EXCEPTION
WHEN OTHERS THEN
NULL;
函数无法正常工作。我四处张望,没有发现任何可以帮助我解决这个问题的东西。我只需要它,它就可以每次检查特定人员是否在线,离线等,并向特定频道发送消息。
答案 0 :(得分: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事件是由每个公会触发的,用户和机器人共享,这意味着如果用户状态更改并与您的机器人共享两个公会,则此代码将执行两次。
答案 1 :(得分:2)
如果您使用在线状态但离线而不是用户在线,我花了整整 2 天的时间寻找答案,所以无论如何我都会分享它
presence.status
上的常见错误是忘记在开发者应用程序中检查这些内容。我不知道是什么意思
现在在你的消息(命令处理程序)函数上......如果你有一个
message.guild.members.cache.get('userId').presence.status
或
${message.author.username} is now ${message.author.presence.status};
如果我发现如何显示所有用户而不是一个用户,我会更新这个
我的第一篇文章...我会记住这个 xD
答案 2 :(得分:-1)
要获得状态,可以使用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');
}
})