I would like to do something in X hours/days. So for example, i would like to send a message on every Monday 8:00 AM. I don't trust my hosting, my BOT is restarting much times. What can i do?
Asked
Active
Viewed 986 times
2 Answers
1
You can use node-schedule to do that.
Or you can create your own scheduler to archive this purpose. Create some schedule struct like this in your database:
{
"nextRun": "2017-01-02T12:00:00Z",
"payload": { /* something for the job */ },
}
In your scheduler, set interval to frequently check for schedule from your database. If the nextRun
matched the current time, execute the job, then calculate and update the nextRun
.

thks173
- 1,500
- 1
- 8
- 6
0
As long as your bot is running a little bit before those times, you can set
function sendMessage(){
// ...
}
bot.on('ready', ()=>{
// ...
let now = new Date();
let target = new Date()
target.setDate(now.getDate() + ((7-now.getDay())%7+1)); // next Monday
target.setHours(8); // eight o'clock
target.setMinutes(0); target.setSeconds(0); target.setMilliseconds(0);
setTimeout(sendMessage, target.now() - now.now());
});
You may want to double check some of the date-checking time, and the next Monday bit is from Getting the date of next Monday.

Holden Rohrer
- 586
- 5
- 16