loads of bug fixes
diff --git a/src/utils/eventScheduler.ts b/src/utils/eventScheduler.ts
index 9210883..396b202 100644
--- a/src/utils/eventScheduler.ts
+++ b/src/utils/eventScheduler.ts
@@ -1,37 +1,70 @@
-import { EventSchedulerDatabase } from './database';
+import { Agenda } from "agenda/es.js";
import client from './client.js';
+import * as fs from 'fs';
+import * as path from 'path';
+import config from '../config/main.json' assert {type: 'json'};
class EventScheduler {
- database: any;
- next: {timestamp: Date, data: object, responded: boolean} | {};
+ private agenda: Agenda;
constructor() {
- this.database = EventSchedulerDatabase;
- this.next = {};
+ this.agenda = new Agenda({db: {address: config.mongoUrl + "Nucleus", collection: 'eventScheduler'}})
+
+ this.agenda.define("unmuteRole", async (job: Agenda.job) => {
+ let guild = await client.guilds.fetch(job.attrs.data.guild);
+ let user = await guild.members.fetch(job.attrs.data.user);
+ let role = await guild.roles.fetch(job.attrs.data.role);
+ await user.roles.remove(role);
+ await job.remove();
+ })
+ this.agenda.define("deleteFile", async (job: Agenda.job) => {
+ fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (err) => {})
+ await job.remove();
+ })
+ this.agenda.define("naturalUnmute", async (job: Agenda.job) => {
+ const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger
+ let guild = await client.guilds.fetch(job.attrs.data.guild);
+ let user = await guild.members.fetch(job.attrs.data.user);
+ if (user.communicationDisabledUntil === null) return
+ try { await client.database.history.create(
+ "unmute", user.guild.id, user.user, null, null, null, null
+ )} catch {}
+ let data = {
+ meta: {
+ type: 'memberUnmute',
+ displayName: 'Unmuted',
+ calculateType: 'guildMemberPunish',
+ color: NucleusColors.green,
+ emoji: "PUNISH.MUTE.GREEN",
+ timestamp: new Date().getTime()
+ },
+ list: {
+ memberId: entry(user.user.id, `\`${user.user.id}\``),
+ name: entry(user.user.id, renderUser(user.user)),
+ unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
+ unmutedBy: entry(null, "*Time out ended*")
+ },
+ hidden: {
+ guild: guild.id
+ }
+ }
+ log(data);
+ })
}
- async create(timestamp: Date, data: object) {
- await this.database.create(timestamp, data);
- if (this.next === {}) {
- this.next = this.next = await this.getNext();
- return
- }
- if (timestamp.getTime() < (this.next as {timestamp: Date}).timestamp.getTime()) {
- this.next = {timestamp: timestamp, data: data, responded: false};
- }
+ async start() {
+ await new Promise(resolve => this.agenda.once('ready', resolve));
+ this.agenda.start()
+ return this
}
- async getNext() {
- let entry = await this.database.getNext();
- if (entry) {
- this.next = entry;
- }
- return this.next;
+ async schedule(name: string, time: string, data: any) {
+ await this.agenda.schedule(time, name, data)
}
- async delete(timestamp: Date, data: object) {
- await this.database.delete(timestamp, data);
- } // TODO: add a loop
+ cancel(name: string, data: Object) {
+ this.agenda.cancel({name, data})
+ }
}
export default EventScheduler;
\ No newline at end of file