blob: 2ef5fb4227bf22a94f8bdbf229aa32ffd1ff3a59 [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) => {
Skyler Greyda16adf2023-03-05 10:22:12 +000026 const channelID: string | null = (await client.database.guilds.read(job.attrs.data.guild)).logging.logs
27 .channel;
TheCodedProf003160f2023-03-04 17:09:40 -050028 if (!channelID) return;
29 const channel = await client.channels.fetch(channelID);
Skyler Greyda16adf2023-03-05 10:22:12 +000030 if (!channel || !(channel instanceof TextChannel)) return;
31 const transcript = await client.database.transcripts.read(
32 job.attrs.data.transcript,
33 job.attrs.data.key,
34 job.attrs.data.iv
35 );
TheCodedProf003160f2023-03-04 17:09:40 -050036 await client.database.transcripts.delete(job.attrs.data.transcript);
37 const file = Buffer.from(JSON.stringify(transcript), "base64");
38 const fileName = `${job.attrs.data.transcript}.json`;
39 const m = await channel.send({ files: [{ attachment: file, name: fileName }] });
Skyler Greyda16adf2023-03-05 10:22:12 +000040 await client.database.transcripts.upload({
41 channelID: channel.id,
42 messageID: m.id,
43 transcript: job.attrs.data.transcript
44 });
TheCodedProf003160f2023-03-04 17:09:40 -050045 await job.remove();
46 });
pineafanbd02b4a2022-08-05 22:01:38 +010047 this.agenda.define("deleteFile", async (job) => {
Skyler Greyda16adf2023-03-05 10:22:12 +000048 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => {
49 client.emit("error", e as Error);
50 });
pineafan73a7c4a2022-07-24 10:38:04 +010051 await job.remove();
pineafan63fc5e22022-08-04 22:04:10 +010052 });
pineafanbd02b4a2022-08-05 22:01:38 +010053 this.agenda.define("naturalUnmute", async (job) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010054 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010055 const guild = await client.guilds.fetch(job.attrs.data.guild);
56 const user = await guild.members.fetch(job.attrs.data.user);
57 if (user.communicationDisabledUntil === null) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010058 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010059 await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null);
Skyler Grey75ea9172022-08-06 10:22:23 +010060 } catch (e) {
PineaFan100df682023-01-02 13:26:08 +000061 client.emit("error", e as Error);
Skyler Grey75ea9172022-08-06 10:22:23 +010062 }
pineafan63fc5e22022-08-04 22:04:10 +010063 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010064 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010065 type: "memberUnmute",
66 displayName: "Unmuted",
67 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010068 color: NucleusColors.green,
69 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -050070 timestamp: Date.now()
pineafan73a7c4a2022-07-24 10:38:04 +010071 },
72 list: {
73 memberId: entry(user.user.id, `\`${user.user.id}\``),
74 name: entry(user.user.id, renderUser(user.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050075 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
pineafan73a7c4a2022-07-24 10:38:04 +010076 unmutedBy: entry(null, "*Time out ended*")
77 },
78 hidden: {
79 guild: guild.id
80 }
pineafan63fc5e22022-08-04 22:04:10 +010081 };
pineafan73a7c4a2022-07-24 10:38:04 +010082 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010083 });
pineafan4edb7762022-06-26 19:21:04 +010084 }
85
pineafan73a7c4a2022-07-24 10:38:04 +010086 async start() {
Skyler Grey75ea9172022-08-06 10:22:23 +010087 await new Promise((resolve) => this.agenda.once("ready", resolve));
pineafan63fc5e22022-08-04 22:04:10 +010088 this.agenda.start();
89 return this;
pineafan4edb7762022-06-26 19:21:04 +010090 }
91
pineafan63fc5e22022-08-04 22:04:10 +010092 // eslint-disable-next-line @typescript-eslint/no-explicit-any
pineafan73a7c4a2022-07-24 10:38:04 +010093 async schedule(name: string, time: string, data: any) {
pineafan63fc5e22022-08-04 22:04:10 +010094 await this.agenda.schedule(time, name, data);
pineafan4edb7762022-06-26 19:21:04 +010095 }
96
pineafan63fc5e22022-08-04 22:04:10 +010097 // eslint-disable-next-line @typescript-eslint/no-explicit-any
98 cancel(name: string, data: any) {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 this.agenda.cancel({ name, data });
pineafan73a7c4a2022-07-24 10:38:04 +0100100 }
pineafan4edb7762022-06-26 19:21:04 +0100101}
102
Skyler Grey75ea9172022-08-06 10:22:23 +0100103export default EventScheduler;