blob: 5c461ad420f66e2c1515de94381a79a4c8d10ad8 [file] [log] [blame]
pineafanbd02b4a2022-08-05 22:01:38 +01001import { Agenda } from "@hokify/agenda";
pineafan63fc5e22022-08-04 22:04:10 +01002import client from "./client.js";
3import * as fs from "fs";
4import * as path from "path";
pineafana2e39c72023-02-21 18:37:32 +00005import config from "../config/main.js";
pineafan4edb7762022-06-26 19:21:04 +01006
7class EventScheduler {
pineafan73a7c4a2022-07-24 10:38:04 +01008 private agenda: Agenda;
pineafan4edb7762022-06-26 19:21:04 +01009
10 constructor() {
Skyler Grey75ea9172022-08-06 10:22:23 +010011 this.agenda = new Agenda({
12 db: {
13 address: config.mongoUrl + "Nucleus",
14 collection: "eventScheduler"
15 }
16 });
pineafanbd02b4a2022-08-05 22:01:38 +010017 this.agenda.define("unmuteRole", async (job) => {
pineafan63fc5e22022-08-04 22:04:10 +010018 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);
PineaFan100df682023-01-02 13:26:08 +000021 if (role) await user.roles.remove(role);
pineafan73a7c4a2022-07-24 10:38:04 +010022 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010023 });
pineafanbd02b4a2022-08-05 22:01:38 +010024 this.agenda.define("deleteFile", async (job) => {
PineaFan100df682023-01-02 13:26:08 +000025 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); });
pineafan73a7c4a2022-07-24 10:38:04 +010026 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010027 });
pineafanbd02b4a2022-08-05 22:01:38 +010028 this.agenda.define("naturalUnmute", async (job) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010029 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010030 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 Grey75ea9172022-08-06 10:22:23 +010033 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010034 await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null);
Skyler Grey75ea9172022-08-06 10:22:23 +010035 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000036 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010037 }
pineafan63fc5e22022-08-04 22:04:10 +010038 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010039 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010040 type: "memberUnmute",
41 displayName: "Unmuted",
42 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010043 color: NucleusColors.green,
44 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -050045 timestamp: Date.now()
pineafan73a7c4a2022-07-24 10:38:04 +010046 },
47 list: {
48 memberId: entry(user.user.id, `\`${user.user.id}\``),
49 name: entry(user.user.id, renderUser(user.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050050 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
pineafan73a7c4a2022-07-24 10:38:04 +010051 unmutedBy: entry(null, "*Time out ended*")
52 },
53 hidden: {
54 guild: guild.id
55 }
pineafan63fc5e22022-08-04 22:04:10 +010056 };
pineafan73a7c4a2022-07-24 10:38:04 +010057 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010058 });
pineafan4edb7762022-06-26 19:21:04 +010059 }
60
pineafan73a7c4a2022-07-24 10:38:04 +010061 async start() {
Skyler Grey75ea9172022-08-06 10:22:23 +010062 await new Promise((resolve) => this.agenda.once("ready", resolve));
pineafan63fc5e22022-08-04 22:04:10 +010063 this.agenda.start();
64 return this;
pineafan4edb7762022-06-26 19:21:04 +010065 }
66
pineafan63fc5e22022-08-04 22:04:10 +010067 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010068 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010069 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010070 }
71
pineafan63fc5e22022-08-04 22:04:10 +010072 // eslint-disable-next-line @typescript-eslint/no-explicit-any
73 cancel(name: string, data: any) {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 this.agenda.cancel({ name, data });
pineafan73a7c4a2022-07-24 10:38:04 +010075 }
pineafan4edb7762022-06-26 19:21:04 +010076}
77
Skyler Grey75ea9172022-08-06 10:22:23 +010078export default EventScheduler;