Haruke Sensei
Haruke Sensei

Reputation: 57

Is there any way to run a discord bot in specific hours of a day?

I want to run my discord.js bot in specific hours of a day like between 6:00 AM to 1:00 PM and then rest for 2 hours and start again from 3:00 PM to 10:00 PM rest again for 2 hours and then 12:00 midnight to 6:00 AM?

Upvotes: 0

Views: 289

Answers (3)

Haruke Sensei
Haruke Sensei

Reputation: 57

You can do this by using this code

setInterval(()=>{
  global.now;
  global.now = new Date().getHours();
}, 5000)

setTimeout(function(){

//you can repeat this code to make it run for different hours

if (global.now >= 6 && global.now <= 18) {

//code here... 

}else{
console.log('Time to Rest'.rainbow)
console.log(global.now)
}

}, 10000)

Upvotes: 0

Lars Rijnen
Lars Rijnen

Reputation: 363

Yes you can do this.

import { Client } from "discord.js";
const app = client
let doStuf = true

setInterval(async () => {
doStuf = !doStuf
if(doStuf) {
app.login(token)
} else {
app.destory()
}

}, 10000)

With this code, your bot is every 10s on and off. You can change the doStuf with a time function. That returns if at this time de bot needs to be offline or online.

Upvotes: 1

Polygon
Polygon

Reputation: 38

Any time a command is initiated, check the time and see if it is in those hours.

Upvotes: 0

Related Questions