blob: f3b7a003a2fd526cfdcf6e4a84a3a3685c7e0b55 [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";
TheCodedProf003160f2023-03-04 17:09:40 -05006import { TextChannel } from "discord.js";
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: {
TheCodedProffaae5332023-03-01 18:16:05 -050014 address: config.mongoOptions.host,
Skyler Grey75ea9172022-08-06 10:22:23 +010015 collection: "eventScheduler"
16 }
17 });
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 });
TheCodedProf003160f2023-03-04 17:09:40 -050025 this.agenda.define("uploadTranscript", async (job) => {
26 const channelID: string | null = (await client.database.guilds.read(job.attrs.data.guild)).logging.logs.channel;
27 if (!channelID) return;
28 const channel = await client.channels.fetch(channelID);
29 if(!channel || !(channel instanceof TextChannel)) return;
30 const transcript = await client.database.transcripts.read(job.attrs.data.transcript, job.attrs.data.key, job.attrs.data.iv);
31 await client.database.transcripts.delete(job.attrs.data.transcript);
32 const file = Buffer.from(JSON.stringify(transcript), "base64");
33 const fileName = `${job.attrs.data.transcript}.json`;
34 const m = await channel.send({ files: [{ attachment: file, name: fileName }] });
35 await client.database.transcripts.upload({ channelID: channel.id, messageID: m.id, transcript: job.attrs.data.transcript})
36 await job.remove();
37 });
pineafanbd02b4a2022-08-05 22:01:38 +010038 this.agenda.define("deleteFile", async (job) => {
PineaFan100df682023-01-02 13:26:08 +000039 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); });
pineafan73a7c4a2022-07-24 10:38:04 +010040 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010041 });
pineafanbd02b4a2022-08-05 22:01:38 +010042 this.agenda.define("naturalUnmute", async (job) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010043 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010044 const guild = await client.guilds.fetch(job.attrs.data.guild);
45 const user = await guild.members.fetch(job.attrs.data.user);
46 if (user.communicationDisabledUntil === null) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010047 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010048 await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null);
Skyler Grey75ea9172022-08-06 10:22:23 +010049 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000050 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010051 }
pineafan63fc5e22022-08-04 22:04:10 +010052 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010053 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010054 type: "memberUnmute",
55 displayName: "Unmuted",
56 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010057 color: NucleusColors.green,
58 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -050059 timestamp: Date.now()
pineafan73a7c4a2022-07-24 10:38:04 +010060 },
61 list: {
62 memberId: entry(user.user.id, `\`${user.user.id}\``),
63 name: entry(user.user.id, renderUser(user.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050064 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
pineafan73a7c4a2022-07-24 10:38:04 +010065 unmutedBy: entry(null, "*Time out ended*")
66 },
67 hidden: {
68 guild: guild.id
69 }
pineafan63fc5e22022-08-04 22:04:10 +010070 };
pineafan73a7c4a2022-07-24 10:38:04 +010071 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010072 });
pineafan4edb7762022-06-26 19:21:04 +010073 }
74
pineafan73a7c4a2022-07-24 10:38:04 +010075 async start() {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 await new Promise((resolve) => this.agenda.once("ready", resolve));
pineafan63fc5e22022-08-04 22:04:10 +010077 this.agenda.start();
78 return this;
pineafan4edb7762022-06-26 19:21:04 +010079 }
80
pineafan63fc5e22022-08-04 22:04:10 +010081 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010082 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010083 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010084 }
85
pineafan63fc5e22022-08-04 22:04:10 +010086 // eslint-disable-next-line @typescript-eslint/no-explicit-any
87 cancel(name: string, data: any) {
Skyler Grey75ea9172022-08-06 10:22:23 +010088 this.agenda.cancel({ name, data });
pineafan73a7c4a2022-07-24 10:38:04 +010089 }
pineafan4edb7762022-06-26 19:21:04 +010090}
91
Skyler Grey75ea9172022-08-06 10:22:23 +010092export default EventScheduler;