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