NodeJS Discord Bot: Detecting User Login

I’m developing a Discord bot using NodeJS and I need help with a specific feature. How can I make my bot react when a particular user comes online? I know the user’s username and ID.

Here’s a basic code snippet I’ve started with, but I’m pretty sure it’s not correct:

const detectUserLogin = {
  name: 'userLoginDetector',
  async execute(client) {
    client.on('presenceUpdate', (oldPresence, newPresence) => {
      if (newPresence.status === 'online' && oldPresence.status !== 'online') {
        const user = newPresence.member.user;
        console.log(`${user.tag} just came online!`);
        // Add your interaction logic here
      }
    });
  },
};

module.exports = detectUserLogin;

Can someone help me understand how to properly detect when a specific user logs in and then have the bot interact with them? Any guidance would be greatly appreciated!

Your approach is close, but there are a few tweaks we can make to improve it. Here’s what I’d suggest based on my experience:

  1. Move the user ID check to the top of your event handler for efficiency.
  2. Use the ‘oldStatus’ and ‘newStatus’ properties directly on the presence objects.
  3. Consider adding a small delay before triggering your action to account for potential network hiccups.

Here’s a refined version of the code:

const TARGET_USER_ID = '123456789';
const DELAY_MS = 2000;

client.on('presenceUpdate', (oldPresence, newPresence) => {
  if (newPresence.userId !== TARGET_USER_ID) return;
  
  if (oldPresence?.status !== 'online' && newPresence.status === 'online') {
    setTimeout(() => {
      if (newPresence.status === 'online') {
        console.log(`${newPresence.user.tag} is now online!`);
        // Your interaction logic here
      }
    }, DELAY_MS);
  }
});

This should give you a more robust solution for detecting when your specific user comes online. Remember to replace ‘123456789’ with the actual user ID you’re targeting.

I’ve implemented something similar in one of my Discord bots, and I can share what worked for me. Your approach is on the right track, but you’ll need to make a few adjustments to target a specific user.

Here’s what I did:

Store the target user’s ID in a variable. In the ‘presenceUpdate’ event, check if the newPresence’s user ID matches your target. Verify the status change from offline to online.

My code looked something like this:

const targetUserId = '123456789';

client.on('presenceUpdate', (oldPresence, newPresence) => {
  if (newPresence.userId === targetUserId) {
    if (!oldPresence || oldPresence.status === 'offline') {
      if (newPresence.status === 'online') {
        console.log('Target user just came online!');
        // Your interaction logic here
      }
    }
  }
});

This setup worked reliably for me. Remember to handle potential errors, such as the user not being in the cache, and consider rate limiting your interactions to avoid accidentally spamming the user.

hey mate, ur code looks pretty good already. one thing tho, u might wanna add a check for the specific user ID ur interested in. like this:

if (newPresence.userId === ‘YOUR_TARGET_USER_ID’ && newPresence.status === ‘online’ && oldPresence?.status !== ‘online’) {
// do ur thing here
}

that should do the trick. good luck with ur bot!