You can remove a specific object from an array like this:
const array = [1, 2, 3];
console.log(array);
//takes away 2 from the array
const index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [1, 3]
console.log(array);
From there, you only have to slightly modify it so that it will simply search for the user mention, which only adds a few extra layers to add. To solve this, I simply grabbed the user ID mentionedID
, then formatted it into a user mention userString
, and then checked it in args
.
Code:
client.on('message', (message) => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'give') {
//user object is first retrieved, never used, but probably will be helpful in your code
let touser = message.mentions.members.first();
//grabs the ID in order to match the args
let mentionedID = message.mentions.users.first().id;
//formats it so that it matches how a user is mentioned
let userString = `<@!${mentionedID}>`
//finds the mention in the array and then removes it
const index = args.indexOf(userString);
if (index > -1) {
args.splice(index,1);
}
//displays the newly modified array
console.log(args);
}
});
Resources:
- How can I remove a specific item from an array?
- Discord.js Documentation