To achieve this task, there are multiple methods, but the fundamental idea is to utilize the Message.mentions.USERS_PATTERN
to verify if the strings in the arguments' array are mentions. If they are, you can choose to remove them or store them in a separate array.
Here is an illustration:
// ASSUMPTIONS:
// args is an array of arguments (strings)
// message is the message that initiated the command
// Discord = require('discord.js'); -> it's the module
let argsWithoutMentions = args.filter(arg => !Discord.MessageMentions.USERS_PATTERN.test(arg));
If you intend to utilize the mentions, you can obtain them with Message.mentions.users
: please be aware that they may not be in the exact order they were sent. If maintaining that order is crucial, you could opt for the following approach:
let argsWithoutMentions = [],
mentions = [];
for (let arg of args) {
if (Discord.MessageMentions.USERS_PATTERN.test(arg)) mentions.push(arg);
else argsWithoutMentions.push(arg);
}
// To utilize those mentions, you'll have to interpret them:
// This function is sourced from the guide referenced earlier (https://github.com/discordjs/guide/blob/master/guide/miscellaneous/parsing-mention-arguments.md)
function getUserFromMention(mention) {
const matches = mention.match(/^<@!?(\d+)>$/);
const id = matches[1];
return client.users.get(id);
}
let mentionedUsers = [];
for (let mention of mentions) {
let user = getUserFromMention(mention);
if (user) mentionedUsers.push(user);
}