diff options
Diffstat (limited to 'src/tasks')
-rw-r--r-- | src/tasks/removePunishmentRole.ts | 32 | ||||
-rw-r--r-- | src/tasks/unban.ts | 31 | ||||
-rw-r--r-- | src/tasks/unmute.ts | 18 |
3 files changed, 47 insertions, 34 deletions
diff --git a/src/tasks/removePunishmentRole.ts b/src/tasks/removePunishmentRole.ts index e69de29..d957238 100644 --- a/src/tasks/removePunishmentRole.ts +++ b/src/tasks/removePunishmentRole.ts @@ -0,0 +1,32 @@ +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); + if (!guild) { + await entry.destroy(); + continue; + } + + const member = guild.members.cache.get(entry.user) as BushGuildMember; + const result = await member.removePunishRole({ reason: 'Punishment expired.', role: entry.role }); + 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 index f5d6b53..136e6c2 100644 --- a/src/tasks/unban.ts +++ b/src/tasks/unban.ts @@ -1,6 +1,4 @@ -import { Ban, BushTask } from '@lib'; -import { DiscordAPIError } from 'discord.js'; -import { Op } from 'sequelize'; +import { BushGuild, BushTask } from '@lib'; export default class UnbanTask extends BushTask { public constructor() { @@ -10,32 +8,19 @@ export default class UnbanTask extends BushTask { }); } async exec(): Promise<void> { - const rows = await Ban.findAll({ - where: { - [Op.and]: [ - { - expires: { - [Op.lt]: new Date() // Find all rows with an expiry date before now - } - } - ] - } - }); + 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); + const guild = this.client.guilds.cache.get(row.guild) as BushGuild; if (!guild) { await row.destroy(); continue; } - try { - await guild.members.unban(row.user, `Unbanning user because tempban expired`); - } catch (e) { - if (e instanceof DiscordAPIError) { - // Member not banned, ignore - } else throw e; - } - await row.destroy(); + + 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 index 7a04360..c61c6e9 100644 --- a/src/tasks/unmute.ts +++ b/src/tasks/unmute.ts @@ -1,5 +1,4 @@ -import { BushTask, Guild, Mute } from '@lib'; -import { DiscordAPIError } from 'discord.js'; +import { BushGuildMember, BushTask, Mute } from '@lib'; import { Op } from 'sequelize'; export default class UnmuteTask extends BushTask { @@ -24,19 +23,16 @@ export default class UnmuteTask extends BushTask { 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); - const muteRole = (await Guild.findByPk(row.guild))?.muteRole; if (!guild) { await row.destroy(); continue; } - try { - await (await guild.members.fetch(row.user)).roles.remove(muteRole); - } catch (e) { - if (e instanceof DiscordAPIError) { - // ignore - } else throw e; - } - await row.destroy(); + + 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}`); } } |