blob: df46ca8a0a793923351e2e097701efdd3c18a576 [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";
5import config from "../config/main.json" assert {type: "json"};
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() {
pineafan63fc5e22022-08-04 22:04:10 +010011 this.agenda = new Agenda({db: {address: config.mongoUrl + "Nucleus", collection: "eventScheduler"}});
pineafan73a7c4a2022-07-24 10:38:04 +010012
pineafanbd02b4a2022-08-05 22:01:38 +010013 this.agenda.define("unmuteRole", async (job) => {
pineafan63fc5e22022-08-04 22:04:10 +010014 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);
pineafan73a7c4a2022-07-24 10:38:04 +010017 await user.roles.remove(role);
18 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010019 });
pineafanbd02b4a2022-08-05 22:01:38 +010020 this.agenda.define("deleteFile", async (job) => {
pineafan63fc5e22022-08-04 22:04:10 +010021 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), client._error);
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("naturalUnmute", async (job) => {
pineafan63fc5e22022-08-04 22:04:10 +010025 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;
pineafan73a7c4a2022-07-24 10:38:04 +010029 try { await client.database.history.create(
30 "unmute", user.guild.id, user.user, null, null, null, null
pineafan63fc5e22022-08-04 22:04:10 +010031 );} catch (e) { client._error(e); }
32 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010033 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010034 type: "memberUnmute",
35 displayName: "Unmuted",
36 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010037 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 }
pineafan63fc5e22022-08-04 22:04:10 +010050 };
pineafan73a7c4a2022-07-24 10:38:04 +010051 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010052 });
pineafan4edb7762022-06-26 19:21:04 +010053 }
54
pineafan73a7c4a2022-07-24 10:38:04 +010055 async start() {
pineafan63fc5e22022-08-04 22:04:10 +010056 await new Promise(resolve => this.agenda.once("ready", resolve));
57 this.agenda.start();
58 return this;
pineafan4edb7762022-06-26 19:21:04 +010059 }
60
pineafan63fc5e22022-08-04 22:04:10 +010061 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010062 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010063 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010064 }
65
pineafan63fc5e22022-08-04 22:04:10 +010066 // eslint-disable-next-line @typescript-eslint/no-explicit-any
67 cancel(name: string, data: any) {
68 this.agenda.cancel({name, data});
pineafan73a7c4a2022-07-24 10:38:04 +010069 }
pineafan4edb7762022-06-26 19:21:04 +010070}
71
72export default EventScheduler;