diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-12-27 13:13:10 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-12-27 13:13:10 -0500 |
commit | 0fa1b2d2a327151f3c9a5c2e28880cf537853302 (patch) | |
tree | 930207facabf2074e4472846b45368412ce0c9ba /src/tasks | |
parent | e05f25f4b98cac3c2409cee9a664ab5ea6251467 (diff) | |
download | tanzanite-0fa1b2d2a327151f3c9a5c2e28880cf537853302.tar.gz tanzanite-0fa1b2d2a327151f3c9a5c2e28880cf537853302.tar.bz2 tanzanite-0fa1b2d2a327151f3c9a5c2e28880cf537853302.zip |
remind command
Diffstat (limited to 'src/tasks')
-rw-r--r-- | src/tasks/handleReminders.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/tasks/handleReminders.ts b/src/tasks/handleReminders.ts new file mode 100644 index 0000000..935a08c --- /dev/null +++ b/src/tasks/handleReminders.ts @@ -0,0 +1,38 @@ +import { BushTask, Reminder } from '#lib'; +const { Op } = (await import('sequelize')).default; + +export default class HandlerRemindersTask extends BushTask { + public constructor() { + super('handlerReminders', { + delay: 30_000, // 30 seconds + runOnStart: true + }); + } + + public override async exec() { + const expiredEntries = await Reminder.findAll({ + where: { + expires: { + [Op.lt]: new Date(Date.now() + 30_000) // Find all rows with an expiry date before 10 seconds from now + }, + notified: false + } + }); + + void client.logger.verbose(`handlerReminders`, `Queried reminders, found <<${expiredEntries.length}>> expired reminders.`); + + for (const entry of expiredEntries) { + setTimeout(() => { + void client.users + .send( + entry.user, + `The reminder you set ${util.dateDelta(entry.created)} ago has expired: ${util.format.bold(entry.content)}\n${ + entry.messageUrl + }` + ) + .catch(() => false); + void entry.update({ notified: true }); + }, entry.expires.getTime() - new Date().getTime()); + } + } +} |