pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 1 | import { Agenda } from "@hokify/agenda"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import client from "./client.js"; |
| 3 | import * as fs from "fs"; |
| 4 | import * as path from "path"; |
pineafan | a2e39c7 | 2023-02-21 18:37:32 +0000 | [diff] [blame] | 5 | import config from "../config/main.js"; |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 6 | import { TextChannel } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 7 | |
| 8 | class EventScheduler { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 9 | private agenda: Agenda; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 10 | |
| 11 | constructor() { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | this.agenda = new Agenda({ |
| 13 | db: { |
TheCodedProf | faae533 | 2023-03-01 18:16:05 -0500 | [diff] [blame] | 14 | address: config.mongoOptions.host, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 15 | collection: "eventScheduler" |
| 16 | } |
| 17 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 18 | this.agenda.define("unmuteRole", async (job) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | 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); |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 22 | if (role) await user.roles.remove(role); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 23 | await job.remove(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 24 | }); |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 25 | this.agenda.define("uploadTranscript", async (job) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 26 | const channelID: string | null = (await client.database.guilds.read(job.attrs.data.guild)).logging.logs |
| 27 | .channel; |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 28 | if (!channelID) return; |
| 29 | const channel = await client.channels.fetch(channelID); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 30 | 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 | ); |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 36 | 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 Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 40 | await client.database.transcripts.upload({ |
| 41 | channelID: channel.id, |
| 42 | messageID: m.id, |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 43 | code: job.attrs.data.transcript |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 44 | }); |
TheCodedProf | 003160f | 2023-03-04 17:09:40 -0500 | [diff] [blame] | 45 | await job.remove(); |
| 46 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 47 | this.agenda.define("deleteFile", async (job) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { |
| 49 | client.emit("error", e as Error); |
| 50 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 51 | await job.remove(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 52 | }); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 53 | this.agenda.define("naturalUnmute", async (job) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 54 | const { log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 59 | await client.database.history.create("unmute", user.guild.id, user.user, null, null, null, null); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | } catch (e) { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 61 | client.emit("error", e as Error); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 64 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | type: "memberUnmute", |
| 66 | displayName: "Unmuted", |
| 67 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 68 | color: NucleusColors.green, |
| 69 | emoji: "PUNISH.MUTE.GREEN", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 70 | timestamp: Date.now() |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 71 | }, |
| 72 | list: { |
| 73 | memberId: entry(user.user.id, `\`${user.user.id}\``), |
| 74 | name: entry(user.user.id, renderUser(user.user)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 75 | unmuted: entry(Date.now().toString(), renderDelta(Date.now())), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 76 | unmutedBy: entry(null, "*Time out ended*") |
| 77 | }, |
| 78 | hidden: { |
| 79 | guild: guild.id |
| 80 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 81 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 82 | await log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 84 | } |
| 85 | |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 86 | async start() { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 87 | await new Promise((resolve) => this.agenda.once("ready", resolve)); |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 88 | await this.agenda.start(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | return this; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 90 | } |
| 91 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 92 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 93 | async schedule(name: string, time: string, data: any) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | await this.agenda.schedule(time, name, data); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | } |
| 96 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 97 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame^] | 98 | async cancel(name: string, data: any) { |
| 99 | await this.agenda.cancel({ name, data }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 100 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | export default EventScheduler; |