我正在尝试在我的 nodejs 脚本中添加一个函数,以允许每 8 小时执行一次选择命令。
例子:
!hug <--- 会让机器人用一个拥抱回应,但每 8 小时只有一次
我一直在网上搜索,但找不到我需要的东西......我试图让它尽可能简化......(即没有mongo......等)
您可以为此使用node-schedule并获得更多功能,您可以在其中配置天、小时和分钟并在满足特定条件时取消,这也使您也可以使用 cron 表达式。
var schedule = require("node-schedule");
var rule = new schedule.RecurrenceRule();
//Will run at 1am 9am and 5pm
rule.hour = [1, 9, 17];
var task = schedule.scheduleJob(rule, function(){
//Do Stuff
console.log("Scheduled Task Running...")
/*
if(condition met)
task.cancel();
*/
});
您可以使用node-cron
var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function() {
/*
* Runs every weekday (Monday through Friday)
* at 11:30:00 AM. It does not run on Saturday
* or Sunday.
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);