本文整理匯總了TypeScript中discord.js.Collection.array方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript js.Collection.array方法的具體用法?TypeScript js.Collection.array怎麽用?TypeScript js.Collection.array使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類discord.js.Collection
的用法示例。
在下文中一共展示了js.Collection.array方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: pickN
function pickN(users: Collection<string, GuildMember>, n: number): string {
const guildMembers = users.array().filter(member => !member.user.bot && member.user.presence.status === "online");
if (guildMembers.length === 0)
return `There are no users in the specified channel.`;
else if (n > guildMembers.length)
return guildMembers.map(member => ` - ${member.displayName}#${member.user.discriminator}`).join("\n");
else {
// Taken from https://jsperf.com/k-random-elements-from-array/2
let result: GuildMember[] = new Array(n);
let taken = new Array(n);
let len = guildMembers.length;
while (n--) {
const randNum = Math.floor(Math.random() * len);
console.log(`RAND: ${randNum}`);
const toAdd = guildMembers[randNum];
result[n] = guildMembers[randNum in taken ? taken[randNum] : randNum];
taken[randNum] = --len;
}
return result.map(member => ` - ${member.displayName}#${member.user.discriminator}`).join("\n");
}
}