blob: 60d525c527168a44d3ef06c84e730582a46d1f2f [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";
Skyler Grey75ea9172022-08-06 10:22:23 +01005import 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() {
Skyler Grey75ea9172022-08-06 10:22:23 +010011 this.agenda = new Agenda({
12 db: {
13 address: config.mongoUrl + "Nucleus",
14 collection: "eventScheduler"
15 }
16 });
pineafan73a7c4a2022-07-24 10:38:04 +010017
pineafanbd02b4a2022-08-05 22:01:38 +010018 this.agenda.define("unmuteRole", async (job) => {
pineafan63fc5e22022-08-04 22:04:10 +010019 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);
PineaFan100df682023-01-02 13:26:08 +000022 if (role) await user.roles.remove(role);
pineafan73a7c4a2022-07-24 10:38:04 +010023 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010024 });
pineafanbd02b4a2022-08-05 22:01:38 +010025 this.agenda.define("deleteFile", async (job) => {
PineaFan100df682023-01-02 13:26:08 +000026 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); });
pineafan73a7c4a2022-07-24 10:38:04 +010027 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010028 });
pineafanbd02b4a2022-08-05 22:01:38 +010029 this.agenda.define("naturalUnmute", async (job) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010030 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010031 const guild = await client.guilds.fetch(job.attrs.data.guild);
32 const user = await guild.members.fetch(job.attrs.data.user);
33 if (user.communicationDisabledUntil === null) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010034 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010035 await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null);
Skyler Grey75ea9172022-08-06 10:22:23 +010036 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000037 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010038 }
pineafan63fc5e22022-08-04 22:04:10 +010039 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010040 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010041 type: "memberUnmute",
42 displayName: "Unmuted",
43 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010044 color: NucleusColors.green,
45 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -050046 timestamp: Date.now()
pineafan73a7c4a2022-07-24 10:38:04 +010047 },
48 list: {
49 memberId: entry(user.user.id, `\`${user.user.id}\``),
50 name: entry(user.user.id, renderUser(user.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050051 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
pineafan73a7c4a2022-07-24 10:38:04 +010052 unmutedBy: entry(null, "*Time out ended*")
53 },
54 hidden: {
55 guild: guild.id
56 }
pineafan63fc5e22022-08-04 22:04:10 +010057 };
pineafan73a7c4a2022-07-24 10:38:04 +010058 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010059 });
pineafan4edb7762022-06-26 19:21:04 +010060 }
61
pineafan73a7c4a2022-07-24 10:38:04 +010062 async start() {
Skyler Grey75ea9172022-08-06 10:22:23 +010063 await new Promise((resolve) => this.agenda.once("ready", resolve));
pineafan63fc5e22022-08-04 22:04:10 +010064 this.agenda.start();
65 return this;
pineafan4edb7762022-06-26 19:21:04 +010066 }
67
pineafan63fc5e22022-08-04 22:04:10 +010068 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010069 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010070 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010071 }
72
pineafan63fc5e22022-08-04 22:04:10 +010073 // eslint-disable-next-line @typescript-eslint/no-explicit-any
74 cancel(name: string, data: any) {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 this.agenda.cancel({ name, data });
pineafan73a7c4a2022-07-24 10:38:04 +010076 }
pineafan4edb7762022-06-26 19:21:04 +010077}
78
Skyler Grey75ea9172022-08-06 10:22:23 +010079export default EventScheduler;