Не определено присутствие пользователя в журнале discord.js

#javascript #discord #discord.js

#javascript #Discord #discord.js

Вопрос:

У меня есть бот под названием Timer Bot, и я хочу, чтобы он входил в систему, когда он переходит в автономный режим и когда он снова подключается к Сети. Я включил намерение присутствия и добавил его в свой клиент

 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 amp;amp; oldPresence.status !== newPresence.status) {
...code
}