blob: 0a32a1ec849f3e76a8d71a2ff68d8dec1db760d8 [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";
PineaFan100df682023-01-02 13:26:08 +00005// @ts-expect-error
Skyler Grey75ea9172022-08-06 10:22:23 +01006import config from "../config/main.json" assert { type: "json" };
pineafan4edb7762022-06-26 19:21:04 +01007
8class EventScheduler {
pineafan73a7c4a2022-07-24 10:38:04 +01009 private agenda: Agenda;
pineafan4edb7762022-06-26 19:21:04 +010010
11 constructor() {
Skyler Grey75ea9172022-08-06 10:22:23 +010012 this.agenda = new Agenda({
13 db: {
14 address: config.mongoUrl + "Nucleus",
15 collection: "eventScheduler"
16 }
17 });
pineafan73a7c4a2022-07-24 10:38:04 +010018
pineafanbd02b4a2022-08-05 22:01:38 +010019 this.agenda.define("unmuteRole", async (job) => {
pineafan63fc5e22022-08-04 22:04:10 +010020 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);
PineaFan100df682023-01-02 13:26:08 +000023 if (role) await user.roles.remove(role);
pineafan73a7c4a2022-07-24 10:38:04 +010024 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010025 });
pineafanbd02b4a2022-08-05 22:01:38 +010026 this.agenda.define("deleteFile", async (job) => {
PineaFan100df682023-01-02 13:26:08 +000027 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); });
pineafan73a7c4a2022-07-24 10:38:04 +010028 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010029 });
pineafanbd02b4a2022-08-05 22:01:38 +010030 this.agenda.define("naturalUnmute", async (job) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010031 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010032 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 Grey75ea9172022-08-06 10:22:23 +010035 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010036 await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null);
Skyler Grey75ea9172022-08-06 10:22:23 +010037 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000038 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010039 }
pineafan63fc5e22022-08-04 22:04:10 +010040 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010041 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010042 type: "memberUnmute",
43 displayName: "Unmuted",
44 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010045 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)),
PineaFan100df682023-01-02 13:26:08 +000052 unmuted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
pineafan73a7c4a2022-07-24 10:38:04 +010053 unmutedBy: entry(null, "*Time out ended*")
54 },
55 hidden: {
56 guild: guild.id
57 }
pineafan63fc5e22022-08-04 22:04:10 +010058 };
pineafan73a7c4a2022-07-24 10:38:04 +010059 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010060 });
pineafan4edb7762022-06-26 19:21:04 +010061 }
62
pineafan73a7c4a2022-07-24 10:38:04 +010063 async start() {
Skyler Grey75ea9172022-08-06 10:22:23 +010064 await new Promise((resolve) => this.agenda.once("ready", resolve));
pineafan63fc5e22022-08-04 22:04:10 +010065 this.agenda.start();
66 return this;
pineafan4edb7762022-06-26 19:21:04 +010067 }
68
pineafan63fc5e22022-08-04 22:04:10 +010069 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010070 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010071 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010072 }
73
pineafan63fc5e22022-08-04 22:04:10 +010074 // eslint-disable-next-line @typescript-eslint/no-explicit-any
75 cancel(name: string, data: any) {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 this.agenda.cancel({ name, data });
pineafan73a7c4a2022-07-24 10:38:04 +010077 }
pineafan4edb7762022-06-26 19:21:04 +010078}
79
Skyler Grey75ea9172022-08-06 10:22:23 +010080export default EventScheduler;