Skip to content Skip to sidebar Skip to footer

Detect If A Bot Account Goes Offline/online

I am using the presenceUpdate event however, it is triggering twice, I am told it is emitting for how many shared servers I have with the bot. Currently my code outputs online twic

Solution 1:

According to discord.js' docspresenceUpdate is Emitted whenever a guild member's presence (e.g. status, activity) is changed.

So it has nothing to do with how many shared servers you have with the bot.

You could try and check for the client's ping using Presence#status every 5 minutes, you could write a function like this:

functioncheckStatus() {
    if (client.users.fetch(botID).presence.status === "online" ) {
   returntrue;
} else {
   returnfalse;
}
}

Then you can run this:

client.setInterval(() =>checkPing(), 300000);

300000 is 5 minutes in milliseconds, so every 5 minutes it'll run the function.

You can read more about client.setInterval()

Solution 2:

I did it by simply comparing the status.

if (oldPresence.status === newPresence.status) return;

Solution 3:

I noticed that this remained unsolved, so I'll solve it for you. So the answers above are correct, however, none of them states the intents the bot requires for this.

You see, presenceUpdate won't emit unless you have enabled the presence privilege gateway intent. For bots in less than 100 servers, this can be toggled on the developer portal by flipping a switch. Otherwise you need to open a ticket for discord to enable the presence intent.

Once you enable intents, try this:

client.on(`presenceUpdate`,(member)=>{
    console.log(`${member.user.username} has updated their presence.`)
})

Then try changing your status to dnd. If you enabled it, you should see [your tag] has updated their presence.. Otherwise you may have done something wrong.

Post a Comment for "Detect If A Bot Account Goes Offline/online"