简体   繁体   English

如何每 10 秒发送一条消息 discord.js?

[英]How to send a message every 10 seconds discord.js?

I am trying to send a message every x amount of seconds in a discord.js bot.我正在尝试在 discord.js bot 中每 x 秒发送一条消息。 I know how to do this put the problem I am having is that it is spamming messages even when I have slowmode enabled.我知道如何做到这一点,我遇到的问题是即使我启用了慢速模式,它也是垃圾邮件。 How can I fix this?我怎样才能解决这个问题?

client.on('message', message => {
if (message.content === '$ww) {
      let count = 0;
      let ecount = 0;
      for(let x = 0; x < 9000; x++) {
        message.channel.send(`hy`)
          .then(m => {
            count++;
          })
          
        }
      }
});

You can use setInterval() to repeat your function every X milliseconds.您可以使用setInterval()每 X 毫秒重复一次您的函数。 For example:例如:

setInterval(() => {
 message.channel.send(`hy`).then(() => count++);
}, 10000);

 setInterval(() => console.log('hey'), 1000)

The code you've provided is spamming because it is not waiting 10 seconds;您提供的代码是垃圾邮件,因为它没有等待 10 秒; it is counting from 0 to 9000 and for every count, it sends the message 'hy', so it quickly spams 9000 'hy' messages.它从 0 到 9000 计数,每次计数时,它都会发送消息“hy”,因此它很快就会发送 9000 条“hy”消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM