var d = new Date();
if(d.getMinutes() === 55)
如果我到达55分钟,如何使机器人发送一次消息?
答案 0 :(得分:1)
示例:
setTimeout(() => { // function
console.log('This will be logged after five seconds')
}, 5000) // amount of time to wait in ms. for this example I used five seconds (5000 ms)
具有排程功能的示例:
function schedule(time, triggerThis) {
// get hour and minute from hour:minute param received, ex.: '16:00'
const hour = Number(time.split(':')[0]);
const minute = Number(time.split(':')[1]);
// create a Date object at the desired timepoint
const startTime = new Date();
startTime.setHours(hour, minute);
const now = new Date();
// increase timepoint by 24 hours if in the past
if (startTime.getTime() < now.getTime()) {
startTime.setHours(startTime.getHours() + 24);
}
// get the interval in ms from now to the timepoint when to trigger the alarm
const firstTriggerAfterMs = startTime.getTime() - now.getTime();
// trigger the function triggerThis() at the timepoint
// create setInterval when the timepoint is reached to trigger it every day at this timepoint
setTimeout(function () {
triggerThis();
setInterval(triggerThis, 24 * 60 * 60 * 1000);
}, firstTriggerAfterMs);
}
schedule('14:00', () => {
// ^^^^^ time to trigger function
// send message
client.channels.cache.get(id) // id of channel you want to send message in
.send('this is a message')
})
答案 1 :(得分:0)
您可以尝试使用cron每55分钟安排一条消息。 https://www.npmjs.com/package/cron