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