Ryan
Ryan

Reputation: 229

Discord.js - How to make a function only run every 30 seconds

I made a bot that replies to a user's message but I want to limit it so that the bot only responds every 30 seconds and ignores the messages within that time. Do I use SetInterval?

Code:

setTimeout(function() {
  main();
}, 30000);

function main(){
      ***command here***
}

This isn't working as well.

Upvotes: 0

Views: 1432

Answers (1)

Syntle
Syntle

Reputation: 5174

Yes, you should use setInterval() for that.

However, I suggest using client.setInterval() and client.setTimeout() instead of just setInterval() and setTimeout() because according to the docs:

Sets an interval that will be automatically cancelled if the client is destroyed.

Which means that if you restart or stop your bot those timers would be cleared.

client.setInterval(function() {
  main();
}, 30000);

Upvotes: 1

Related Questions