我尝试制作一个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');
}
您对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();