简体   繁体   中英

discord.js - How to pick random mentioned users?

I am trying to make system, that will dm mentioned users and author of the message with random message. But I'm stuck on getting all mentioned users from the message.

 const userName = message.author.username;
 const userName1 = message.mentions.users.get(1);
 const userName2 = message.mentions.users.get(2);
 const userName3 = message.mentions.users.get(3);

 const userId = message.author.id;

 message.channel.send(`Test ${userName}\nTest1 ${userName1}\nTest2 ${userName2}`)

You can call Map on the mentions collection by each user's username and assign it to a usernames array. Then use each element in that array.

const userName = message.author.username
const usernames = message.mentions.users.map(u => u.username)

const userId = message.author.id

message.channel.send(`Test ${userName}\nTest1 ${usernames[0]}\nTest2 ${usernames[1]}`)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM