提问者:小点点

如何从服务器中随机挑选用户?


我尝试制作一个bot命令,回复“(随机用户)是bot”。 我尝试过其他解决方案,但都不管用。 下面是我的代码:

if (msg.content === 'wb!bot') {
        let userArray = Array.from(msg.member.guild.members);
        let randomUser = userArray[Math.floor(Math.random() * msg.guild.memberCount)];

        console.log(randomUser);
        console.log(userArray);

        msg.channel.send(randomUser + ' is bot');
}

共2个答案

匿名用户

您对Math.Random的使用不正确。

null

// Generate the array for the snippet
const userArray = [];

for (let i = 0 ; i < 100 ; ++i) {
  userArray.push(`user${i + 1}`);
}

// Function that returns you a random number
// https://www.w3schools.com/js/js_random.asp
function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}

// Pick a random user
const user = userArray[getRndInteger(0, userArray.length)];

// Display it
console.log(`${user} is bot`);

匿名用户

collection类已经具有.random()方法时,使用Math.Random是没有意义的

const usersCollection = msg.guild.members;
const randomUser = usersCollection.random();