Skip to content
Advertisement

error when a non bot member comes online (presenceUpdate)

I am trying to get my bot to send this message when a bot goes online or offline. it works sometimes but when a human member comes online i get this error: UNCAUGHT EXCEPTION TypeError: Cannot read property 'user' of undefined.
My code:

client.on("presenceUpdate",(oldPresence,newPresence)=>{
    if(newPresence.user.bot){
        client.channels.cache.get("799082492065087498").send(oldPresence.user.tag+" is "+newPresence.status);
    };
});

Advertisement

Answer

If we assume that this code is server side: Definitely you didn’t send newPresence in client side that you get this Error. So you can check newPresence before newPresence.user. Like this:

client.on("presenceUpdate",(oldPresence,newPresence)=>{
    if(newPresence && newPresence.user.bot){
        client.channels.cache.get("799082492065087498").send(oldPresence.user.tag+" is "+newPresence.status);
    };
});
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement