aboutsummaryrefslogtreecommitdiff
path: root/src/tasks/unmute.ts
blob: 2dfa2ab12f7421209c4d93e8f25cc6d046501016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { DiscordAPIError } from 'discord.js';
import { Op } from 'sequelize';
import { BushTask } from '../lib/extensions/BushTask';
import { Guild } from '../lib/models';
import { Mute } from '../lib/models/Mute';

export default class UnmuteTask extends BushTask {
	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);
			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();
			this.client.logger.verbose(`UnmuteTask`, `Unmuted ${row.user}`);
		}
	}
}