JavaScript Discord Bot - Unable to Delete User ID from Array

I’m working on a Discord bot that tracks online users, but I’m having trouble removing user IDs from my array when they go offline.

client.on("presenceUpdate", (previousPresence, currentPresence) => {
  if(previousPresence.status !== currentPresence.status){
    const userID = currentPresence.user.id;
    if(currentPresence.status === "online"){      
      activeUsers.push(userID);
      console.log("-------user online--------");
      console.log(activeUsers);
    }else
    if(currentPresence.status === "offline"){
      activeUsers.filter(id => id !== userID)
      console.log("-------user offline--------");
      console.log(activeUsers);
    }
      console.log(`${currentPresence.user.username} status changed to ${currentPresence.status}`);
  }
});

The issue is that user IDs aren’t being removed from the array. Here’s what I see in the console:

-------user offline-------- []
-TestUser- is now offline
-------user online-------- [ '203287818330570752' ]
-TestUser- is now online
-------user offline-------- [ '203287818330570752' ]
-TestUser- is now offline
-------user online-------- [ '203287818330570752', '203287818330570752' ]
-TestUser- is now online

As you can see, the ID stays in the array and even gets duplicated. What am I doing wrong?

The issue is that filter creates a new array instead of changing the original one. You need to assign the result back: activeUsers = activeUsers.filter(id => id !== userID); and it’ll work properly. I made this exact mistake building my moderation bot last year - wasted hours debugging before I realized I wasn’t reassigning the filtered array. The duplicates happen because you’re adding users multiple times without checking if they’re already there.

Your filter method isn’t working because you’re not reassigning the result. The original array stays unchanged. Fix it with activeUsers = activeUsers.filter(id => id !== userID). Also, check for duplicates when users come online using if (!activeUsers.includes(userID)) before pushing to the array. Had the same issue with my first Discord bot - took me forever to figure out.

yep, filter doesn’t change the original array, ya gotta save it back. or use splice: activeUsers.splice(activeUsers.indexOf(userID), 1) but don’t forget to check if indexOf isn’t -1 first or it’ll screw up your array.