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"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 5 | import config from "../config/main.json" assert { type: "json" }; |
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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 22 | await user.roles.remove(role); |
| 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) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 26 | fs.rm( |
| 27 | path.resolve("dist/utils/temp", job.attrs.data.fileName), |
| 28 | client._error |
| 29 | ); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 30 | await job.remove(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 32 | this.agenda.define("naturalUnmute", async (job) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 33 | const { log, NucleusColors, entry, renderUser, renderDelta } = |
| 34 | client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | const guild = await client.guilds.fetch(job.attrs.data.guild); |
| 36 | const user = await guild.members.fetch(job.attrs.data.user); |
| 37 | if (user.communicationDisabledUntil === null) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 38 | try { |
| 39 | await client.database.history.create( |
| 40 | "unmute", |
| 41 | user.guild.id, |
| 42 | user.user, |
| 43 | null, |
| 44 | null, |
| 45 | null, |
| 46 | null |
| 47 | ); |
| 48 | } catch (e) { |
| 49 | client._error(e); |
| 50 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 52 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | type: "memberUnmute", |
| 54 | displayName: "Unmuted", |
| 55 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 56 | color: NucleusColors.green, |
| 57 | emoji: "PUNISH.MUTE.GREEN", |
| 58 | timestamp: new Date().getTime() |
| 59 | }, |
| 60 | list: { |
| 61 | memberId: entry(user.user.id, `\`${user.user.id}\``), |
| 62 | name: entry(user.user.id, renderUser(user.user)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 63 | unmuted: entry( |
| 64 | new Date().getTime(), |
| 65 | renderDelta(new Date().getTime()) |
| 66 | ), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 67 | unmutedBy: entry(null, "*Time out ended*") |
| 68 | }, |
| 69 | hidden: { |
| 70 | guild: guild.id |
| 71 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 73 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 75 | } |
| 76 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 77 | async start() { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 78 | await new Promise((resolve) => this.agenda.once("ready", resolve)); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | this.agenda.start(); |
| 80 | return this; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 81 | } |
| 82 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 84 | async schedule(name: string, time: string, data: any) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | await this.agenda.schedule(time, name, data); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 86 | } |
| 87 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 88 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 89 | cancel(name: string, data: any) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 90 | this.agenda.cancel({ name, data }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 91 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 94 | export default EventScheduler; |