diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-07-11 18:44:09 +0200 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-07-11 18:44:09 +0200 |
commit | d2a020837267bf024f508b402f596f8e242672ed (patch) | |
tree | 7e371015fa1451bda993e44aa591b3cc24c3209d /src/tasks/feature/handleReminders.ts | |
parent | 7d1dede862339c439830a0306e2b7f3a273f967f (diff) | |
download | tanzanite-d2a020837267bf024f508b402f596f8e242672ed.tar.gz tanzanite-d2a020837267bf024f508b402f596f8e242672ed.tar.bz2 tanzanite-d2a020837267bf024f508b402f596f8e242672ed.zip |
create task categories
Diffstat (limited to 'src/tasks/feature/handleReminders.ts')
-rw-r--r-- | src/tasks/feature/handleReminders.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/tasks/feature/handleReminders.ts b/src/tasks/feature/handleReminders.ts new file mode 100644 index 0000000..7863c9a --- /dev/null +++ b/src/tasks/feature/handleReminders.ts @@ -0,0 +1,39 @@ +import { BushTask, dateDelta, format, Reminder, Time } from '#lib'; +const { Op } = (await import('sequelize')).default; + +export default class HandlerRemindersTask extends BushTask { + public constructor() { + super('handlerReminders', { + delay: 30 * Time.Second, + runOnStart: true + }); + } + + public async exec() { + const expiredEntries = await Reminder.findAll({ + where: { + expires: { + [Op.lt]: new Date(Date.now() + 30 * Time.Second) // Find all rows with an expiry date before 30 seconds from now + }, + notified: false + } + }); + + void this.client.logger.verbose( + `handlerReminders`, + `Queried reminders, found <<${expiredEntries.length}>> expired reminders.` + ); + + for (const entry of expiredEntries) { + setTimeout(() => { + void this.client.users + .send( + entry.user, + `The reminder you set ${dateDelta(entry.created)} ago has expired: ${format.bold(entry.content)}\n${entry.messageUrl}` + ) + .catch(() => false); + void entry.update({ notified: true }); + }, entry.expires.getTime() - new Date().getTime()); + } + } +} |