pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 1 | import { Agenda } from "agenda/es.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 2 | import client from './client.js'; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 3 | import * as fs from 'fs'; |
| 4 | import * as path from 'path'; |
| 5 | import config from '../config/main.json' assert {type: 'json'}; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | |
| 7 | class EventScheduler { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 8 | private agenda: Agenda; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 9 | |
| 10 | constructor() { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 11 | 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 | }) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | } |
| 54 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 55 | async start() { |
| 56 | await new Promise(resolve => this.agenda.once('ready', resolve)); |
| 57 | this.agenda.start() |
| 58 | return this |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 59 | } |
| 60 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 61 | async schedule(name: string, time: string, data: any) { |
| 62 | await this.agenda.schedule(time, name, data) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 63 | } |
| 64 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 65 | cancel(name: string, data: Object) { |
| 66 | this.agenda.cancel({name, data}) |
| 67 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | export default EventScheduler; |