我如何检查一个人是否在discord.js中上线 ,下线等?

编程入门 行业动态 更新时间:2024-10-06 22:26:39

我如何检查一个人是否在discord.js中<a href=https://www.elefans.com/category/jswz/34/1768703.html style=上线 ,下线等?"/>

我如何检查一个人是否在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中上线 ,下线等?

本文发布于:2024-05-07 21:30:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上线   discord   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!