Skip to content
Advertisement

How to detect if the script is trying to add the two same IDs?

I’m trying to make a logger for IDs and names, but I can’t seem to detect if the script is trying to add the two same IDs.

I’ve tried id.includes(user._id), but it always passed the if test. Here’s the code:

let id = [];
MPP.client.on('participant added', user => {
    if (id.includes(user._id))
        return console.log(
            `%c - joined, seen ${user._id}, n: ${user.name}`,
            'color: red;'
        );
    id.push({ id: user._id, n: user.name, action: 'joined' });
    console.log(
        `%c + joined, added ${user._id} to list, n: ${user.name}`,
        'color: lime;'
    );
});
MPP.client.on('participant removed', user => {
    if (id.includes(user._id))
        return console.log(
            `%c - left, seen ${user._id}, n: ${user.name}`,
            'color: red;'
        );
    id.push({ id: user._id, n: user.name, action: 'left' });
    console.log(
        `%c + left, added ${user._id} to list, n: ${user.name}`,
        'color: lime;'
    );
});

Advertisement

Answer

You can search your id array with find this will give you the current object which you can match your user._id against.

if (id.find(participant => participant.id === user._id))
  return ...
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement