简体   繁体   中英

How do you select a random member id from a server? Discord.js

I have tried a lot of stuff and researched as much as I can, I can't find anything that works or I can get inspiration from. Anything that I find doesn't work and I need help. I just want to get a random member from id (maybe create an array of the ids). This is the closest I have gotten/best thing I can think of:

const members = await message.guild.members.fetch();
for (const [, member] of members) {
  console.log(member.id);
};

member.id gets all the ids of the users in the server but I don't know what else to do with that(like make it into an array). Can anyone help?

You can try and use .random() on the members collection like so:

const members = await message.guild.members.fetch();
const randMember = members.random(); 
console.log(randMember);

If you want an array of random members, you can pass a number into the .random() method to specify how many random members you want, eg: .random(5) will give 5 random members.

Here, members is a Collection where the member IDs are keys. You can use the .randomKey() method to get a random ID. So, your line might be const randomId = members.randomKey(1) or something like that.

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