blob: 6923ede9f7049dfccf4a1d234b4daf87715cf2a5 [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);
pineafan73a7c4a2022-07-24 10:38:04 +010022 await user.roles.remove(role);
23 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010024 });
pineafanbd02b4a2022-08-05 22:01:38 +010025 this.agenda.define("deleteFile", async (job) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010026 fs.rm(
27 path.resolve("dist/utils/temp", job.attrs.data.fileName),
28 client._error
29 );
pineafan73a7c4a2022-07-24 10:38:04 +010030 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010031 });
pineafanbd02b4a2022-08-05 22:01:38 +010032 this.agenda.define("naturalUnmute", async (job) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010033 const { log, NucleusColors, entry, renderUser, renderDelta } =
34 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010035 const guild = await client.guilds.fetch(job.attrs.data.guild);
36 const user = await guild.members.fetch(job.attrs.data.user);
37 if (user.communicationDisabledUntil === null) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010038 try {
39 await client.database.history.create(
40 "unmute",
41 user.guild.id,
42 user.user,
43 null,
44 null,
45 null,
46 null
47 );
48 } catch (e) {
49 client._error(e);
50 }
pineafan63fc5e22022-08-04 22:04:10 +010051 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010052 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010053 type: "memberUnmute",
54 displayName: "Unmuted",
55 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010056 color: NucleusColors.green,
57 emoji: "PUNISH.MUTE.GREEN",
58 timestamp: new Date().getTime()
59 },
60 list: {
61 memberId: entry(user.user.id, `\`${user.user.id}\``),
62 name: entry(user.user.id, renderUser(user.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +010063 unmuted: entry(
64 new Date().getTime(),
65 renderDelta(new Date().getTime())
66 ),
pineafan73a7c4a2022-07-24 10:38:04 +010067 unmutedBy: entry(null, "*Time out ended*")
68 },
69 hidden: {
70 guild: guild.id
71 }
pineafan63fc5e22022-08-04 22:04:10 +010072 };
pineafan73a7c4a2022-07-24 10:38:04 +010073 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010074 });
pineafan4edb7762022-06-26 19:21:04 +010075 }
76
pineafan73a7c4a2022-07-24 10:38:04 +010077 async start() {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 await new Promise((resolve) => this.agenda.once("ready", resolve));
pineafan63fc5e22022-08-04 22:04:10 +010079 this.agenda.start();
80 return this;
pineafan4edb7762022-06-26 19:21:04 +010081 }
82
pineafan63fc5e22022-08-04 22:04:10 +010083 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010084 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010085 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010086 }
87
pineafan63fc5e22022-08-04 22:04:10 +010088 // eslint-disable-next-line @typescript-eslint/no-explicit-any
89 cancel(name: string, data: any) {
Skyler Grey75ea9172022-08-06 10:22:23 +010090 this.agenda.cancel({ name, data });
pineafan73a7c4a2022-07-24 10:38:04 +010091 }
pineafan4edb7762022-06-26 19:21:04 +010092}
93
Skyler Grey75ea9172022-08-06 10:22:23 +010094export default EventScheduler;