blob: 396b202269793c7e1820857182bc43c034a0e586 [file] [log] [blame]
pineafan73a7c4a2022-07-24 10:38:04 +01001import { Agenda } from "agenda/es.js";
pineafan4edb7762022-06-26 19:21:04 +01002import client from './client.js';
pineafan73a7c4a2022-07-24 10:38:04 +01003import * 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() {
pineafan73a7c4a2022-07-24 10:38:04 +010011 this.agenda = new Agenda({db: {address: config.mongoUrl + "Nucleus", collection: 'eventScheduler'}})
12
13 this.agenda.define("unmuteRole", async (job: Agenda.job) => {
14 let guild = await client.guilds.fetch(job.attrs.data.guild);
15 let user = await guild.members.fetch(job.attrs.data.user);
16 let role = await guild.roles.fetch(job.attrs.data.role);
17 await user.roles.remove(role);
18 await job.remove();
19 })
20 this.agenda.define("deleteFile", async (job: Agenda.job) => {
21 fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (err) => {})
22 await job.remove();
23 })
24 this.agenda.define("naturalUnmute", async (job: Agenda.job) => {
25 const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
26 let guild = await client.guilds.fetch(job.attrs.data.guild);
27 let user = await guild.members.fetch(job.attrs.data.user);
28 if (user.communicationDisabledUntil === null) return
29 try { await client.database.history.create(
30 "unmute", user.guild.id, user.user, null, null, null, null
31 )} catch {}
32 let data = {
33 meta: {
34 type: 'memberUnmute',
35 displayName: 'Unmuted',
36 calculateType: 'guildMemberPunish',
37 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 }
50 }
51 log(data);
52 })
pineafan4edb7762022-06-26 19:21:04 +010053 }
54
pineafan73a7c4a2022-07-24 10:38:04 +010055 async start() {
56 await new Promise(resolve => this.agenda.once('ready', resolve));
57 this.agenda.start()
58 return this
pineafan4edb7762022-06-26 19:21:04 +010059 }
60
pineafan73a7c4a2022-07-24 10:38:04 +010061 async schedule(name: string, time: string, data: any) {
62 await this.agenda.schedule(time, name, data)
pineafan4edb7762022-06-26 19:21:04 +010063 }
64
pineafan73a7c4a2022-07-24 10:38:04 +010065 cancel(name: string, data: Object) {
66 this.agenda.cancel({name, data})
67 }
pineafan4edb7762022-06-26 19:21:04 +010068}
69
70export default EventScheduler;