Search code examples
javascriptnode.jsdiscorddiscord.js

discordjs v14 TypeError: Cannot read properties of undefined (reading 'get')


I'm having an issue with this Cannot read properties of undefined (reading 'get').

I have tried many things but none work. It does work when I don't work with module exports.

Here is my full code

const { EmbedBuilder} = require('discord.js');

isstreaming = false;

module.exports = {
    name: "presenceUpdate",
    async execute(newPresence, client) {
        if (isstreaming === false) {
            if (newPresence.user.id === '151762679659233280') {
                const stream = newPresence.activities.find((activity) => activity.type === 1 && activity.name === 'Twitch');
                if (stream) {
                    const twitchEmbed = new EmbedBuilder()
                        .setTitle(`${newPresence.user.username} is now live on Twitch!`)
                        .setColor('#6441A5')
                        .setDescription(`${stream.details}\n\nWatch now: ${stream.url}`);
                    
                    const channel = client.channels.get('1092538530879787160')
                    channel.send({ embeds: [twitchEmbed] })
                    isstreaming = true;
                }
            }
        }
        if (newPresence.user.id === '151762679659233280') {
            const notstream = newPresence.activities.find((activity) => activity.type === 0);
            if (notstream) {
                isstreaming = false;
            }
        }
    }
}

Here is full error:

TypeError: Cannot read properties of undefined (reading 'get')
    at Object.execute (C:\Users\john\Desktop\RastaFam discord bot\Events\twitch.js:17:53)
    at Client.<anonymous> (C:\Users\john\Desktop\RastaFam discord bot\Handlers\eventHandler.js:23:59)
    at Client.emit (node:events:512:28)
    at PresenceUpdateAction.handle (C:\Users\john\Desktop\RastaFam discord bot\node_modules\discord.js\src\client\actions\PresenceUpdate.js:37:19)
    at module.exports [as PRESENCE_UPDATE] (C:\Users\john\Desktop\RastaFam discord bot\node_modules\discord.js\src\client\websocket\handlers\PRESENCE_UPDATE.js:4:33)
    at WebSocketManager.handlePacket (C:\Users\john\Desktop\RastaFam discord bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (C:\Users\john\Desktop\RastaFam discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:494:22)
    at WebSocketShard.onMessage (C:\Users\john\Desktop\RastaFam discord bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10)
    at callListener (C:\Users\john\Desktop\RastaFam discord bot\node_modules\ws\lib\event-target.js:290:14)
    at WebSocket.onMessage (C:\Users\john\Desktop\RastaFam discord bot\node_modules\ws\lib\event-target.js:209:9)


Solution

  • I think it's because presenceUpdate takes two parameters (oldPresence and newPresence) and you only used the first one. So your client variable is actually the presence after the update and newPresence is the one before the update.

    You'll probably need to use something like:

    async execute(oldPresence, newPresence, client) { /* ... */ }
    

    You could also use const channel = newPresence.client.channels.get('1092538530879787160').