简体   繁体   English

您如何 select 来自服务器的随机成员 ID? Discord.js

[英]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).我只想从 id 中获取一个随机成员(也许创建一个 id 数组)。 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). member.id 获取服务器中用户的所有 id,但我不知道还能做什么(比如将其放入数组中)。 Can anyone help?任何人都可以帮忙吗?

You can try and use .random() on the members collection like so:您可以尝试在成员集合上使用.random() ,如下所示:

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.如果你想要一个随机成员数组,你可以将一个数字传递给.random()方法来指定你想要多少个随机成员,例如: .random(5)将给出 5 个随机成员。

Here, members is a Collection where the member IDs are keys.这里, members是一个集合,其中成员 ID 是键。 You can use the .randomKey() method to get a random ID.您可以使用.randomKey()方法获取随机 ID。 So, your line might be const randomId = members.randomKey(1) or something like that.因此,您的行可能是const randomId = members.randomKey(1)或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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