I'm using discord.js.
I'm trying to make a bot command that will output the following output:
Welcome, (mentioned user 1) and (mentioned user 2), enjoy your stay!
When the syntax is:
(prefix) getaroom for (mentioned user 1) (mentioned user 2)
Help?!
Although message.mentions.users.first()
works well for the first mentioned user when it comes to wanting a second, third or maybe even fourth we start running into various issues.
The way I prefer resolving this issue is by running the .forEach()
function through message.mentions.users
.
Example:
var prefix = 'yourPrefixHere'; //Defining your prefix
client.on('message', message => { //Adding your event listener
if (message.content.toLowerCase().startsWith(prefix + 'getaroom') { //When the command is entered
if (!message.mentions.first()) return message.channel.send(`You have not defined your users.`); //If the message did not include mentioned users
var count = 0; //To find out what user we're on.
let user1; //Defining the users
let user2; //Defining the users
message.mentions.forEach(user => {
count++; //Adding one onto the count variable
if (count >= 3) return; //If the user mentioned more than two users return
if (count === 1) user1 = message.guild.members.cache.get(user.id); //Getting the first mentioned user
else user2 = message.guild.members.cache.get(user.id); //Getting the second mentioned user
});
//Rest of your code...
}
}
You can convert the message.mentions.users
collection to an array of user IDs with Collection.prototype.keyArray()
.
const [first, second] = message.mentions.users.keyArray();
if (!first || !second)
return message.channel.send('You need to mention two users!');
message.channel.send(`Welcome, <@${first}> and <@${second}>, enjoy your stay!`);
If you convert them to a string with the @ symbol in front of it does it work. I'm pretty sure this used to work when I worked on bots in python.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.