上线 ,下线等?"/>
我如何检查一个人是否在discord.js中上线 ,下线等?
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()”函数不起作用。我四处张望,没有发现任何可以解决此问题的方法。我只需要它,它就可以每次检查特定人员是否在线,离线等,并向特定频道发送消息。
回答如下:首先,要遵守的一个规则是事件侦听器应该始终位于代码的顶层,并且永不嵌套。 (否则,您可能会遇到内存泄漏和其他问题,例如重复执行和意外执行代码)。
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事件,用户和机器人共享,这意味着如果用户状态更改并与您的机器人共享两个行会,则此代码将执行两次。] >>
更多推荐
我如何检查一个人是否在discord.js中上线 ,下线等?
发布评论