没有setInterval()的计划消息 [英] Scheduled messages without setInterval()

查看:66
本文介绍了没有setInterval()的计划消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想安排一些诸如与不和谐的机器人发送消息之类的事情。

例如:我希望该机器人每天早上8点发送早安或宣布一些事情。

I want to schedule some things like sending messages with my discord bot.
E.g.: I want the bot to send "Good Morning" every day at 8 am or announce some things.

我的问题是:我不能使用 setInterval()之类的东西每24小时执行一次,因为如果机器人离线或必须重新启动,否则将重置或延迟时间间隔。

My problem is: I can't use something like setInterval() to execute every 24 hours because if the bot goes offline or has to be restarted it would reset or delay the interval.

问题:如何在特定时间执行某些操作而不必担心机器人有时会离线?

Question: How do I execute something at a specific point in time without having to worry about the bot sometimes being offline?

推荐答案

您可以使用 cron 软件包:您安排了每天在特定时间运行的作业(时间将在系统时钟上读取,

You can use the cron package: you schedule a job that runs every day at a specific hour (time will be read on the system clock, you'll have to figure it out by yourself for timezones).

以下是每天上午8:00发送邮件的示例。

Here's an example of a message being sent every day at 8:00 am.

const cron = require('cron');

const channel; // Let's say this is the channel where you want to send it.
const job = new cron.CronJob('0 0 8 * * *', () => {
  channel.send("It's 8:00 am.");
});

关于 0 0 8 * * * 模式:其格式为第二分钟小时月日日月日工作日

您可以找到有关cron模式的更多信息此处

About the 0 0 8 * * * pattern: its format is second minute hour month-day month week-day.
You can find more about cron patterns here.

这篇关于没有setInterval()的计划消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆