有没有办法在一天的特定时间 运行 一个不和谐的机器人?
Is there any way to run a discord bot in specific hours of a day?
我想 运行 我的 discord.js 机器人在一天的特定时间,例如 6:00 上午到 1:00 下午之间,然后休息 2 小时,然后重新开始从 3:00 下午到 10:00 下午再次休息 2 小时,然后 12:00 午夜到 6:00 上午?
任何时候启动一个命令,检查时间,看看它是否在那个时间。
是的,你可以做到这一点。
import { Client } from "discord.js";
const app = client
let doStuf = true
setInterval(async () => {
doStuf = !doStuf
if(doStuf) {
app.login(token)
} else {
app.destory()
}
}, 10000)
使用此代码,您的机器人每 10 秒开启和关闭一次。您可以使用时间功能更改 doStuf。那returns如果此时de bot需要离线或在线。
您可以使用此代码执行此操作
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)
我想 运行 我的 discord.js 机器人在一天的特定时间,例如 6:00 上午到 1:00 下午之间,然后休息 2 小时,然后重新开始从 3:00 下午到 10:00 下午再次休息 2 小时,然后 12:00 午夜到 6:00 上午?
任何时候启动一个命令,检查时间,看看它是否在那个时间。
是的,你可以做到这一点。
import { Client } from "discord.js";
const app = client
let doStuf = true
setInterval(async () => {
doStuf = !doStuf
if(doStuf) {
app.login(token)
} else {
app.destory()
}
}, 10000)
使用此代码,您的机器人每 10 秒开启和关闭一次。您可以使用时间功能更改 doStuf。那returns如果此时de bot需要离线或在线。
您可以使用此代码执行此操作
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)