首页 > 解决方案 > 定时器完成时需要通知,在下一个间隔之前

问题描述

我想在计时器完成时收到通知,而不仅仅是“OFFSET”,有人可以帮助我吗?谢谢。

编辑:所以该脚本基本上是一个计时器,假设每隔一小时又一分钟在我的不和谐频道上给我一个通知。现在我只收到一个通知,目标距离 X 分钟。当实际计时器完成时(在下一个间隔之前),我想要另一个通知。

编辑2:我不想创建另一个主题,所以任何人都可以帮我添加秒吗?现在是唯一的小时和分钟计时器,我想增加几秒钟。所以改为:20:50 我想要:20:50:20

var Discord = require("discord.js");
var bot = new Discord.Client();

var NOTIFY_CHANNEL;
bot.on('ready', () => {
    NOTIFY_CHANNEL = bot.channels.cache.get('xxx'); // Channel to send notification
});

const START_DATE = '2020-08-15'; // Date used as the starting point for multi-hour intervals, must be YYYY-MM-DD format
const START_HOUR = 19; // Hour of the day when the timer begins (0 is 12am, 23 is 11pm), used with START_DATE and INTERVAL_HOURS param
const INTERVAL_HOURS = 1; // Trigger at an interval of every X hours
const TARGET_MINUTE = 1; // Minute of the hour when the chest will refresh, 30 means 1:30, 2:30, etc.
const OFFSET = 5; // Notification will warn that the target is X minutes away


const NOTIFY_MINUTE = (TARGET_MINUTE < OFFSET ? 60 : 0) + TARGET_MINUTE - OFFSET;
console.log('Notification sent');
const START_TIME = new Date(new Date(START_DATE).getTime() + new Date().getTimezoneOffset() * 60000 + START_HOUR * 3600000).getTime();

setInterval(function () {
    var d = new Date();
    if (Math.floor((d.getTime() - START_TIME) / 3600000) % INTERVAL_HOURS > 0) return; // Return if hour is not the correct interval
    if (d.getMinutes() !== NOTIFY_MINUTE) return; // Return if current minute is not the notify minute
    NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');
}, 60 * 1000); // Check every minute


bot.login('xxx');

标签: javascriptnode.jsdiscord.js

解决方案


为此,您要删除

if (d.getMinutes() !== NOTIFY_MINUTE)

更改您已有的计时器

NOTIFY_CHANNEL.send('in: ' + OFFSET + '分钟');

if (d.getMinutes() == NOTIFY_MINUTE) NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');

并添加

if (d.getMinutes() == TARGET_MINUTE) NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');

在目标分钟通知,

这给我们留下了

setInterval(function () {
    var d = new Date();
    if (Math.floor((d.getTime() - START_TIME) / 3600000) % INTERVAL_HOURS > 0) return; // Return if hour is not the correct interval
    // if (d.getMinutes() !== NOTIFY_MINUTE) return; // Return if current minute is not the notify minute
    if (d.getMinutes() == NOTIFY_MINUTE) NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');
    if (d.getMinutes() == TARGET_MINUTE) NOTIFY_CHANNEL.send("It's now");
}, 60 * 1000); // Check every minute

推荐阅读