pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 1 | import { Agenda } from "agenda/es.js"; |
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"; |
| 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() { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 11 | this.agenda = new Agenda({db: {address: config.mongoUrl + "Nucleus", collection: "eventScheduler"}}); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 12 | |
| 13 | this.agenda.define("unmuteRole", async (job: Agenda.job) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 14 | const guild = await client.guilds.fetch(job.attrs.data.guild); |
| 15 | const user = await guild.members.fetch(job.attrs.data.user); |
| 16 | const role = await guild.roles.fetch(job.attrs.data.role); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 17 | await user.roles.remove(role); |
| 18 | await job.remove(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 19 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 20 | this.agenda.define("deleteFile", async (job: Agenda.job) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 21 | fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), client._error); |
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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 24 | this.agenda.define("naturalUnmute", async (job: Agenda.job) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 25 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 26 | const guild = await client.guilds.fetch(job.attrs.data.guild); |
| 27 | const user = await guild.members.fetch(job.attrs.data.user); |
| 28 | if (user.communicationDisabledUntil === null) return; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 29 | try { await client.database.history.create( |
| 30 | "unmute", user.guild.id, user.user, null, null, null, null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 31 | );} catch (e) { client._error(e); } |
| 32 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 33 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 34 | type: "memberUnmute", |
| 35 | displayName: "Unmuted", |
| 36 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 37 | color: NucleusColors.green, |
| 38 | emoji: "PUNISH.MUTE.GREEN", |
| 39 | timestamp: new Date().getTime() |
| 40 | }, |
| 41 | list: { |
| 42 | memberId: entry(user.user.id, `\`${user.user.id}\``), |
| 43 | name: entry(user.user.id, renderUser(user.user)), |
| 44 | unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 45 | unmutedBy: entry(null, "*Time out ended*") |
| 46 | }, |
| 47 | hidden: { |
| 48 | guild: guild.id |
| 49 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 50 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 51 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 52 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | } |
| 54 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 55 | async start() { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 56 | await new Promise(resolve => this.agenda.once("ready", resolve)); |
| 57 | this.agenda.start(); |
| 58 | return this; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 59 | } |
| 60 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 61 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 62 | async schedule(name: string, time: string, data: any) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 63 | await this.agenda.schedule(time, name, data); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 64 | } |
| 65 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 66 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 67 | cancel(name: string, data: any) { |
| 68 | this.agenda.cancel({name, data}); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 69 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | export default EventScheduler; |