-1

I currently just have a simple table full of users who have entered the command: ~afk
Example:

var afkMembers = []
client.on('message' async message => {
if (message.content.toLowerCase() === '~afk') {
afkMembers.push(message.author.id)
return message.channel.send('you are now marked as AFK')
}

if (message.content.toLowerCase() === '~back') {
//remove the element from the table
return message.channel.send('you are now marked as back')
}

});


I'm just wondering how I would remove the users value from the table.
It is probably quite simple, however I cannot find a solution.
Thanks.

Tyler2P
  • 1,391
  • 1
  • 9
  • 18

3 Answers3

1

You can remove a certain element from in array with Array.prototype.splice()

const index = afkMembers.indexOf(message.author.id);
if (index === -1) return message.channel.send('Your are not marked as AFK');
afkMembers.splice(index, 1);

However, I would actually suggest rewriting both commands and using a Set instead of an Array. Sets are unique because a value in a set may only appear once; there are no duplicates. It also has unique methods that would be helpful such as Set.add(), Set.delete(), and Set.has()

const afkMembers = new Set();

client.on("message", async (message) => {
  if (message.content.toLowerCase() === "~afk") {
    if (afkMembers.has(message.author.id))
      return message.channel.send("You are already marked as AFK!");

    afkMembers.add(message.author.id);
    message.channel.send("you are now marked as AFK");

  } else if (message.content.toLowerCase() === "~back") {
    if (!afkMembers.has(message.author.id))
      return message.channel.send("You are not marked as AFK!");

    afkMembers.delete(message.author.id);
    message.channel.send("You are now marked as back");
  }
});

Lioness100
  • 7,179
  • 5
  • 9
  • 43
0

I think you can remove the value like this :

afkMembers.splice(afkMembers.indexOf(message.author.id), 1);
Xylah
  • 11
  • 3
0

It seems like you're simply manipulating an array, If I'm not missing something in the explanation, I would use splice to remove the user.

afkMembers.splice(afkMembers.indexOf(message.author.id), 1);

audzzy
  • 733
  • 1
  • 2
  • 12