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