gpt4 book ai didi

node.js - 我如何在discord.js中检查一个人是否上线、离线等?

转载 作者:行者123 更新时间:2023-12-02 19:30:21 25 4
gpt4 key购买 nike

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() 该功能不起作用。我到处寻找,但没有发现任何可以帮助我解决这个问题的方法。我只需要它,这样它每次都会检查特定人员是否上线、离线等。并向特定 channel 发送消息。

最佳答案

首先,要遵守的一条规则是事件监听器应始终位于代码的顶层且切勿嵌套。 (否则您可能会遇到内存泄漏和其他问题,例如重复和意外的代码执行)。

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事件由用户和机器人共享的每个公会触发,这意味着如果用户状态更改并与您的机器人共享两个公会,则此代码将执行两次。

关于node.js - 我如何在discord.js中检查一个人是否上线、离线等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61914382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com