diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-01-15 19:21:32 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-01-15 19:21:32 -0500 |
commit | 9a2002a1368ba9ae53ef7419a87213f8efd142ee (patch) | |
tree | 6cbf63a70bf15e390a896cbce18a1f9ff26dbf2c | |
parent | 8e35f109d4b3fc1779e2cd6c3aba5a4d4ee1da85 (diff) | |
download | tanzanite-9a2002a1368ba9ae53ef7419a87213f8efd142ee.tar.gz tanzanite-9a2002a1368ba9ae53ef7419a87213f8efd142ee.tar.bz2 tanzanite-9a2002a1368ba9ae53ef7419a87213f8efd142ee.zip |
make expired punishments be removed at the exact time it expires
-rw-r--r-- | src/tasks/handleReminders.ts | 2 | ||||
-rw-r--r-- | src/tasks/removeExpiredPunishements.ts | 94 |
2 files changed, 50 insertions, 46 deletions
diff --git a/src/tasks/handleReminders.ts b/src/tasks/handleReminders.ts index 935a08c..d1af480 100644 --- a/src/tasks/handleReminders.ts +++ b/src/tasks/handleReminders.ts @@ -13,7 +13,7 @@ export default class HandlerRemindersTask extends BushTask { 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 + [Op.lt]: new Date(Date.now() + 30_000) // Find all rows with an expiry date before 30 seconds from now }, notified: false } diff --git a/src/tasks/removeExpiredPunishements.ts b/src/tasks/removeExpiredPunishements.ts index 394d866..904e614 100644 --- a/src/tasks/removeExpiredPunishements.ts +++ b/src/tasks/removeExpiredPunishements.ts @@ -14,7 +14,7 @@ export default class RemoveExpiredPunishmentsTask extends BushTask { const expiredEntries = await ActivePunishment.findAll({ where: { expires: { - [Op.lt]: new Date() // Find all rows with an expiry date before now + [Op.lt]: new Date(Date.now() + 15_000) // Find all rows with an expiry date before 15 seconds from now } } }); @@ -26,55 +26,59 @@ export default class RemoveExpiredPunishmentsTask extends BushTask { for (const entry of expiredEntries) { const guild = client.guilds.cache.get(entry.guild) as BushGuild; - const member = guild.members.cache.get(entry.user); - const user = (await util.resolveNonCachedUser(entry.user)) as BushUser; - if (!guild) continue; - switch (entry.type) { - case ActivePunishmentType.BAN: { - assert(user); - const result = await guild.bushUnban({ user: user, reason: 'Punishment expired' }); - if (['success', 'user not banned'].includes(result)) await entry.destroy(); - else throw new Error(result); - void client.logger.verbose(`removeExpiredPunishments`, `Unbanned ${entry.user}.`); - break; - } - case ActivePunishmentType.BLOCK: { - if (!member) { - await entry.destroy(); // channel overrides are removed when the member leaves the guild - continue; + // eslint-disable-next-line @typescript-eslint/no-misused-promises + setTimeout(async () => { + const member = guild.members.cache.get(entry.user); + const user = (await util.resolveNonCachedUser(entry.user)) as BushUser; + assert(guild); + + switch (entry.type) { + case ActivePunishmentType.BAN: { + assert(user); + const result = await guild.bushUnban({ user: user, reason: 'Punishment expired' }); + if (['success', 'user not banned'].includes(result)) await entry.destroy(); + else throw new Error(result); + void client.logger.verbose(`removeExpiredPunishments`, `Unbanned ${entry.user}.`); + break; } - const result = await member.bushUnblock({ reason: 'Punishment expired', channel: entry.extraInfo }); - if (['success', 'user not blocked'].includes(result)) await entry.destroy(); - else throw new Error(result); - void client.logger.verbose(`removeExpiredPunishments`, `Unblocked ${entry.user}.`); - break; - } - case ActivePunishmentType.MUTE: { - if (!member) continue; - const result = await member.bushUnmute({ reason: 'Punishment expired' }); - if (['success', 'failed to dm'].includes(result)) await entry.destroy(); - else throw new Error(result); - void client.logger.verbose(`removeExpiredPunishments`, `Unmuted ${entry.user}.`); - break; - } - case ActivePunishmentType.ROLE: { - if (!member) continue; - const role = guild?.roles?.cache?.get(entry.extraInfo); - if (!role) throw new Error(`Cannot unmute ${member.user.tag} because I cannot find the mute role.`); - const result = await member.bushRemoveRole({ - reason: 'Punishment expired', - role: role, - addToModlog: true - }); + case ActivePunishmentType.BLOCK: { + if (!member) { + await entry.destroy(); // channel overrides are removed when the member leaves the guild + return; + } + const result = await member.bushUnblock({ reason: 'Punishment expired', channel: entry.extraInfo }); + if (['success', 'user not blocked'].includes(result)) await entry.destroy(); + else throw new Error(result); + void client.logger.verbose(`removeExpiredPunishments`, `Unblocked ${entry.user}.`); + break; + } + case ActivePunishmentType.MUTE: { + if (!member) return; + const result = await member.bushUnmute({ reason: 'Punishment expired' }); + if (['success', 'failed to dm'].includes(result)) await entry.destroy(); + else throw new Error(result); + void client.logger.verbose(`removeExpiredPunishments`, `Unmuted ${entry.user}.`); + break; + } + case ActivePunishmentType.ROLE: { + if (!member) return; + const role = guild?.roles?.cache?.get(entry.extraInfo); + if (!role) throw new Error(`Cannot unmute ${member.user.tag} because I cannot find the mute role.`); + const result = await member.bushRemoveRole({ + reason: 'Punishment expired', + role: role, + addToModlog: true + }); - if (['success', 'failed to dm'].includes(result)) await entry.destroy(); - else throw new Error(result); - void client.logger.verbose(`removeExpiredPunishments`, `Removed a punishment role from ${entry.user}.`); - break; + if (['success', 'failed to dm'].includes(result)) await entry.destroy(); + else throw new Error(result); + void client.logger.verbose(`removeExpiredPunishments`, `Removed a punishment role from ${entry.user}.`); + break; + } } - } + }, entry.expires!.getTime() - new Date().getTime()); } } } |