[英]It's sends 4 times the amount of members
So I want my bot in a certain server to send a dm to a random member every 10 minutes.所以我希望某个服务器中的机器人每 10 分钟向随机成员发送一个 dm。 And when my bot has sent everyone from the server a dm it sends a complete message.当我的机器人从服务器向每个人发送 dm 时,它会发送一条完整的消息。
But when i start the bot it sends 4 times the amount of members但是当我启动机器人时,它会发送 4 倍的成员数量
if (message.content.startsWith(botconfig.prefix + 'dmall')) {
console.log("demo");
var list = message.guild.members.array();
sendMessage(list);
}
});
function sendMessage(list) {
setTimeout(function () {
for (i = 0; i < list.length; i++) {
console.log(list.length);
}
console.log("I'm done, mate!");
sendMessage(list);
}, 10 * 1000);
}
CONSOLE:安慰:
demo
4 (is the amount of the members)
4
4
4
I'm done mate!
This part of your code:这部分代码:
for (i = 0; i < list.length; i++) {
console.log(list.length);
}
tells Javascript to run the statement:告诉 Javascript 运行语句:
console.log(list.length);
list.length
times. list.length
次。 If list.length
is 4
which it appears to be here, then you will see in the console如果list.length
是4
,它似乎在这里,那么您将在控制台中看到
4
4
4
4
That's what the code was instructed to do.这就是代码被指示做的事情。
I don't see any reason why you'd put that in a loop unless you want to output each array element separately.除非您想分别输出每个数组元素,否则我看不出有任何理由将其放入循环中。 So, if you want to just output the length once, then replace this:所以,如果你只想输出一次长度,那么替换这个:
for (i = 0; i < list.length; i++) {
console.log(list.length);
}
with this:有了这个:
console.log(list.length);
In addition, if you were to use a for
loop, you MUST declare all variables that you use.此外,如果您要使用for
循环,则必须声明您使用的所有变量。 So, this:所以这:
for (i = 0; i < list.length; i++) {
is very dangerous.非常危险。 It relies on a higher scoped i
which can easily conflict with other higher scoped i
variables and create hard-to-figure out bugs.它依赖于更高范围的i
,它很容易与其他更高范围的i
变量发生冲突并产生难以解决的错误。 Every for
loop should declare it's own loop variable as in:每个for
循环都应该声明它自己的循环变量,如下所示:
for (let i = 0; i < list.length; i++) {
If you run your code in strict mode (which you should), the above for loop declaration would likely cause an error (which is a good thing because you'd immediately see the coding error and fix it).如果你在严格模式下运行你的代码(你应该这样做),上面的 for 循环声明可能会导致一个错误(这是一件好事,因为你会立即看到编码错误并修复它)。
There are a couple of things wrong here.这里有一些错误。 For of: JavaScript variable scoping messing you up ( Different example here对于:JavaScript 变量作用域让你一团糟( 这里有不同的例子
Second of all: setTimeout is not the same as setInterval.其次:setTimeout 与 setInterval 不同。
Completely fixing your issue would be:完全解决您的问题将是:
// Use the discord API let users = ['justme'] function getUsers() { // Mutate just to display a new one each time users = [...users, users.length.toString()]; return users } function notifyUsers() { // Retrieve all users now const currentUsers = getUsers() // Send a message to each user currentUsers.forEach(user => sendMessage(user)) console.log('I have sent everyone a message') } function sendMessage(user) { console.log(`Sending message to ${user}`) } // Start the application by looping every <whatever> setInterval(() => notifyUsers(), 1000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.