简体   繁体   English

它的发送数量是会员数量的 4 倍

[英]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.length4 ,它似乎在这里,那么您将在控制台中看到

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.

相关问题 我需要限制这个 for 循环发送的请求数量 - I need to throttle the amount of requests this for loop sends 如何收集我的项目中使用第 3 方依赖项的次数及其方法? - How can I collect the amount of times a 3rd party dependency is used in my project and it's methods? 为什么亚马逊多次发送对SNS的确认请求 - Why Amazon sends confirmation request for SNS several times 我的机器人私下多次发送相同的消息 - My bot sends the same message several times in private S3 copyObject 发送拒绝访问 - S3 copyObject sends Access Denied requestretry如何使用json正文发送请求? - How's requestretry sends request with json body? 在函数中发送给每个人,并在完成后发送完整的消息 - Dm everyone in a function and sends a complete message when it's done AppEngine nodejs 应用偶尔发送 502 并重启 - AppEngine nodejs app sporadically sends 502s and restarts 每当用户添加新数据时,节点就会多次发送回数据 - Node sends data back multiple times everytime a user adds new data 我的浏览器连续发送获取数据请求,并在响应 200 次后给出挂起状态 - My browser sends fetch data request continuously and it gives pending status after giving 200 several times in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM