记录用户存在未识别的 discord.js

Posted

技术标签:

【中文标题】记录用户存在未识别的 discord.js【英文标题】:Logging user presence unidentified discord.js 【发布时间】:2021-03-27 03:52:45 【问题描述】:

我有一个名为 Timer Bot 的机器人,我希望它在离线和重新在线时登录。我已启用 Presence Intent 并将其添加到我的客户端

Intents = Discord.Intents
const client = new Discord.Client(ws:intents: [Intents.FLAGS.GUILD_PRESENCES]);

当我运行这个脚本时 -

client.on('presenceUpdate', (oldPresence, newPresence) => 
    let member = newPresence.member;
    if (member.id === '603517534720753686') 
        if (oldPresence.status !== newPresence.status) 
            let channel = member.guild.channels.cache.get('788547135234375712');
            let text = "";
            if (newPresence.status === "online") 
                text = "**Hello @everyone, Timer Bot is now online! Thank you for your patience.**";
             else if (newPresence.status === "offline") 
                text = "**@everyone Due to issues, Timer Bot is currently offline. We apologize for the inconvenience.**";
            
            channel.send(text);
        
    
);

我的控制台显示 -

Logged in as Timer Bot Utilities#6525!
/home/runner/TimerUtilities/index.js:25
        if (oldPresence.status !== newPresence.status) 
                        ^

TypeError: Cannot read property 'status' of undefined
    at Client.<anonymous> (/home/runner/TimerUtilities/index.js:25:25)
    at Client.emit (events.js:315:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at PresenceUpdateAction.handle (/home/runner/TimerUtilities/node_modules/discord.js/src/client/actions/PresenceUpdate.js:39:19)
    at Object.module.exports [as PRESENCE_UPDATE] (/home/runner/TimerUtilities/node_modules/discord.js/src/client/websocket/handlers/PRESENCE_UPDATE.js:4:33)
    at WebSocketManager.handlePacket (/home/runner/TimerUtilities/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at Immediate.<anonymous> (/home/runner/TimerUtilities/node_modules/discord.js/src/client/websocket/WebSocketManager.js:379:14)
    at processImmediate (internal/timers.js:456:21)

oldPresence 似乎身份不明。有谁知道为什么?

谢谢, 布赖恩

【问题讨论】:

【参考方案1】:

因为oldPresence 可能为null,这就是status返回undefined的原因

if (oldPresence && oldPresence.status !== newPresence.status) 
...code

【讨论】:

以上是关于记录用户存在未识别的 discord.js的主要内容,如果未能解决你的问题,请参考以下文章

未识别的 404 未找到页面

PHP - 未识别的索引变量[重复]

专用结构/类中未识别的数据成员

我的笔记本的本地连接有两个网络,一个网络二,一个未识别的网络,该怎么办??

使用 map() 在 lm 中未识别的因素

jQuery.key 返回未识别的 Android 键盘