Files

76 lines
2.0 KiB
JavaScript

const { mClient } = require('../index');
function getRemaining(timer) {
if (timer.state !== 'running') {
return timer.timeRemaining;
}
return Math.max(
0,
timer.timeRemaining - (Date.now() - timer.startedAt)
);
}
module.exports = {
name: 'timerCheck',
async execute(client) {
const db = mClient.db('neo-db');
const wgeColl = db.collection('timer_wge');
const timers = await wgeColl.find({
state: { $ne: 'stopped' }
}).toArray();
const now = Date.now();
for (const timer of timers) {
const remaining = getRemaining(timer);
// -------------------------
// HALF TIME (3.5 days left)
// -------------------------
if (
!timer.notifications?.half &&
remaining <= 3.5 * 24 * 60 * 60 * 1000
) {
await wgeColl.updateOne(
{ _id: timer._id },
{ $set: { "notifications.half": true } }
);
client.emit('timerHalf', timer);
}
// -------------------------
// 24H WARNING
// -------------------------
if (
!timer.notifications?.day24 &&
remaining <= 24 * 60 * 60 * 1000
) {
await wgeColl.updateOne(
{ _id: timer._id },
{ $set: { "notifications.day24": true } }
);
client.emit('timer24h', timer);
}
// -------------------------
// COMPLETION
// -------------------------
if (
!timer.notifications?.complete &&
remaining <= 0
) {
await wgeColl.updateOne(
{ _id: timer._id },
{ $set: { "notifications.complete": true } }
);
client.emit('timerComplete', timer);
}
}
}
};