pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 1 | import { Agenda } from "@hokify/agenda"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import client from "./client.js"; |
| 3 | import * as fs from "fs"; |
| 4 | import * as path from "path"; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 5 | import config from "../config/main.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | |
| 7 | class EventScheduler { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 8 | private agenda: Agenda; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 9 | |
| 10 | constructor() { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 11 | this.agenda = new Agenda({ |
| 12 | db: { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 13 | address: config.mongoOptions.host, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 14 | collection: "eventScheduler" |
| 15 | } |
| 16 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 17 | this.agenda.define("unmuteRole", async (job) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | const guild = await client.guilds.fetch(job.attrs.data.guild); |
| 19 | const user = await guild.members.fetch(job.attrs.data.user); |
| 20 | const role = await guild.roles.fetch(job.attrs.data.role); |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 21 | if (role) await user.roles.remove(role); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 22 | await job.remove(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 24 | this.agenda.define("deleteFile", async (job) => { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 25 | fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 26 | await job.remove(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 28 | this.agenda.define("naturalUnmute", async (job) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 29 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | const guild = await client.guilds.fetch(job.attrs.data.guild); |
| 31 | const user = await guild.members.fetch(job.attrs.data.user); |
| 32 | if (user.communicationDisabledUntil === null) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 34 | await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | } catch (e) { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 36 | client.emit("error", e as Error); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 37 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 39 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | type: "memberUnmute", |
| 41 | displayName: "Unmuted", |
| 42 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 43 | color: NucleusColors.green, |
| 44 | emoji: "PUNISH.MUTE.GREEN", |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 45 | timestamp: Date.now() |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 46 | }, |
| 47 | list: { |
| 48 | memberId: entry(user.user.id, `\`${user.user.id}\``), |
| 49 | name: entry(user.user.id, renderUser(user.user)), |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame] | 50 | unmuted: entry(Date.now().toString(), renderDelta(Date.now())), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 51 | unmutedBy: entry(null, "*Time out ended*") |
| 52 | }, |
| 53 | hidden: { |
| 54 | guild: guild.id |
| 55 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 56 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 57 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 58 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 59 | } |
| 60 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 61 | async start() { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | await new Promise((resolve) => this.agenda.once("ready", resolve)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | this.agenda.start(); |
| 64 | return this; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 65 | } |
| 66 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 67 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 68 | async schedule(name: string, time: string, data: any) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | await this.agenda.schedule(time, name, data); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 70 | } |
| 71 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 73 | cancel(name: string, data: any) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | this.agenda.cancel({ name, data }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 75 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | export default EventScheduler; |