pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import client from "./client.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 2 | import EmojiEmbed from "./generateEmojiEmbed.js"; |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 3 | import { Record as ImmutableRecord } from "immutable"; |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 4 | import type { TextChannel, ThreadChannel, NewsChannel } from "discord.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 5 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 6 | const severitiesType = ImmutableRecord({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 7 | Critical: "Danger", |
| 8 | Warning: "Warning", |
| 9 | Info: "Success" |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 10 | } as Record<string, "Danger" | "Warning" | "Success">); |
| 11 | const severities = severitiesType(); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 12 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 13 | export default async function ( |
| 14 | type: string, |
| 15 | guild: string, |
| 16 | message: string | true, |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 17 | severity: "Critical" | "Warning" | "Info" = "Info", |
| 18 | pings?: string[] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | const data = await client.database.guilds.read(guild); |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 21 | if (data.logging.staff.channel === null) return; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 22 | if (message === true) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | return await client.database.guilds.write(guild, { |
| 24 | [`singleEventNotifications.${type}`]: false |
| 25 | }); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 26 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 27 | if (data.singleEventNotifications[type]) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 28 | await client.database.guilds.write(guild, { |
| 29 | [`singleEventNotifications.${type}`]: true |
| 30 | }); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 31 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | const channel = await client.channels.fetch(data.logging.staff.channel); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 33 | if (!channel) return; |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 34 | if (!channel.isTextBased()) return; |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 35 | const textChannel = channel as TextChannel | ThreadChannel | NewsChannel; |
| 36 | let messageData = {embeds: [ |
| 37 | new EmojiEmbed() |
| 38 | .setTitle(`${severity} notification`) |
| 39 | .setDescription(message) |
| 40 | .setStatus(severities.get(severity)) |
| 41 | .setEmoji("CONTROL.BLOCKCROSS") |
| 42 | ]} |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 43 | if (pings) { |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 44 | messageData = Object.assign(messageData, { |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 45 | content: pings.map((ping) => `<@${ping}>`).join(" ") |
| 46 | }); |
| 47 | } |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 48 | await textChannel.send(messageData); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 49 | } catch (err) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | console.error(err); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 51 | } |
| 52 | } |