1

so I have this "touser" variable that defines the target's userid when the message author types it in args[0] but i want to be able to grab it in both the first and last arg depending on where it is.

My CMD syntax is:

#give [userid/mention] [item] [item] [item]

I also want it to work like this:

#give [item] [item] [item] [userid/mention]

My code right now:

let touser = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
xerosa
  • 31
  • 5

1 Answers1

1

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:

  1. How can I remove a specific item from an array?
  2. Discord.js Documentation
PerplexingParadox
  • 929
  • 1
  • 4
  • 20