diff options
Diffstat (limited to 'src/tasks')
-rw-r--r-- | src/tasks/removeExpiredPunishements.ts | 74 | ||||
-rw-r--r-- | src/tasks/removePunishmentRole.ts | 37 | ||||
-rw-r--r-- | src/tasks/unban.ts | 27 | ||||
-rw-r--r-- | src/tasks/unmute.ts | 39 |
4 files changed, 74 insertions, 103 deletions
diff --git a/src/tasks/removeExpiredPunishements.ts b/src/tasks/removeExpiredPunishements.ts new file mode 100644 index 0000000..d0220ba --- /dev/null +++ b/src/tasks/removeExpiredPunishements.ts @@ -0,0 +1,74 @@ +import { BushGuild, BushGuildMember, BushTask } from '@lib'; +import { Op } from 'sequelize'; +import { ActivePunishment, ActivePunishmentType } from '../lib/models/ActivePunishment'; + +export default class RemoveExpiredPunishmentsTask extends BushTask { + public constructor() { + super('removeExpiredPunishments', { + delay: 15_000, // 15 seconds + runOnStart: true + }); + } + async exec(): Promise<void> { + const expiredEntries = await ActivePunishment.findAll({ + where: { + [Op.and]: [ + { + expires: { + [Op.lt]: new Date() // Find all rows with an expiry date before now + } + } + ] + } + }); + + this.client.logger.verbose( + `removeExpiredPunishments`, + `Queried punishments, found <<${expiredEntries.length}>> expired punishments.` + ); + + for (const entry of expiredEntries) { + const guild = this.client.guilds.cache.get(entry.guild) as BushGuild; + const member = guild.members.cache.get(entry.user) as BushGuildMember; + + if (!guild) { + await entry.destroy(); + continue; + } + + switch (entry.type) { + case ActivePunishmentType.BAN: { + const result = await guild.unban({ user: entry.user, reason: 'Punishment expired.' }); + if (['success', 'user not banned'].includes(result)) await entry.destroy(); + else throw result; + this.client.logger.verbose(`removeExpiredPunishments`, `Unbanned ${entry.user}.`); + break; + } + case ActivePunishmentType.BLOCK: { + //todo + break; + } + case ActivePunishmentType.MUTE: { + const result = await member.unmute({ reason: 'Punishment expired.' }); + if (['success', 'failed to dm'].includes(result)) await entry.destroy(); + else throw result; + this.client.logger.verbose(`removeExpiredPunishments`, `Unmuted ${entry.user}.`); + break; + } + case ActivePunishmentType.ROLE: { + const role = guild?.roles?.cache?.get(entry.extraInfo); + const result = await member.removeRole({ + reason: 'Punishment expired.', + role: role, + addToModlog: true + }); + + if (['success', 'failed to dm'].includes(result)) await entry.destroy(); + else throw result; + this.client.logger.verbose(`removeExpiredPunishments`, `Removed a punishment role from ${entry.user}.`); + break; + } + } + } + } +} diff --git a/src/tasks/removePunishmentRole.ts b/src/tasks/removePunishmentRole.ts deleted file mode 100644 index 9830338..0000000 --- a/src/tasks/removePunishmentRole.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { BushGuildMember, BushTask } from '@lib'; - -export default class RemovePunishmentRole extends BushTask { - public constructor() { - super('removePunishmentRole', { - delay: 30_000, // 1/2 min - runOnStart: true - }); - } - async exec(): Promise<void> { - const expiredEntries = await this.client.util.findExpiredEntries('role'); - this.client.logger.verbose( - `RemovePunishmentRoleTask`, - `Queried punishment roles, found <<${expiredEntries.length}>> expired punishment roles.` - ); - - for (const entry of expiredEntries) { - const guild = this.client.guilds.cache.get(entry.guild); - const role = guild?.roles?.cache?.get(entry.role); - if (!guild || !role) { - await entry.destroy(); - continue; - } - - const member = guild.members.cache.get(entry.user) as BushGuildMember; - const result = await member.removeRole({ - reason: 'Punishment expired.', - role: role, - addToModlog: true - }); - if (['success', 'failed to dm'].includes(result)) await entry.destroy(); - else throw result; - - this.client.logger.verbose(`RemovePunishmentRoleTask`, `Removed a punishment role from ${entry.user}.`); - } - } -} diff --git a/src/tasks/unban.ts b/src/tasks/unban.ts deleted file mode 100644 index 136e6c2..0000000 --- a/src/tasks/unban.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { BushGuild, BushTask } from '@lib'; - -export default class UnbanTask extends BushTask { - public constructor() { - super('unban', { - delay: 30_000, // 1/2 min - runOnStart: true - }); - } - async exec(): Promise<void> { - const rows = await this.client.util.findExpiredEntries('mute'); - this.client.logger.verbose(`UnbanTask`, `Queried bans, found <<${rows.length}>> expired bans.`); - - for (const row of rows) { - const guild = this.client.guilds.cache.get(row.guild) as BushGuild; - if (!guild) { - await row.destroy(); - continue; - } - - const result = await guild.unban({ user: row.user, reason: 'Punishment expired.' }); - if (['success', 'user not banned'].includes(result)) await row.destroy(); - else throw result; - this.client.logger.verbose(`UnbanTask`, `Unbanned ${row.user}`); - } - } -} diff --git a/src/tasks/unmute.ts b/src/tasks/unmute.ts deleted file mode 100644 index c61c6e9..0000000 --- a/src/tasks/unmute.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { BushGuildMember, BushTask, Mute } from '@lib'; -import { Op } from 'sequelize'; - -export default class UnmuteTask extends BushTask { - public constructor() { - super('unmute', { - delay: 30_000, // 1/2 min - runOnStart: true - }); - } - async exec(): Promise<void> { - const rows = await Mute.findAll({ - where: { - [Op.and]: [ - { - expires: { - [Op.lt]: new Date() // Find all rows with an expiry date before now - } - } - ] - } - }); - this.client.logger.verbose(`UnmuteTask`, `Queried mutes, found <<${rows.length}>> expired mutes.`); - for (const row of rows) { - const guild = this.client.guilds.cache.get(row.guild); - if (!guild) { - await row.destroy(); - continue; - } - - const member = guild.members.cache.get(row.user) as BushGuildMember; - const result = await member.unmute({ reason: 'Punishment expired.' }); - if (['success', 'failed to dm'].includes(result)) await row.destroy(); - else throw result; - - this.client.logger.verbose(`UnmuteTask`, `Unmuted ${row.user}`); - } - } -} |