blob: 8e3aa6044dd5eb7d9de4ea42598c1482cb037c59 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import client from "./client.js";
pineafan4edb7762022-06-26 19:21:04 +01002import EmojiEmbed from "./generateEmojiEmbed.js";
pineafanbd02b4a2022-08-05 22:01:38 +01003import { Record as ImmutableRecord } from "immutable";
pineafanad54d752022-04-18 19:01:43 +01004
pineafanbd02b4a2022-08-05 22:01:38 +01005const severitiesType = ImmutableRecord({
Skyler Grey75ea9172022-08-06 10:22:23 +01006 Critical: "Danger",
7 Warning: "Warning",
8 Info: "Success"
pineafanbd02b4a2022-08-05 22:01:38 +01009} as Record<string, "Danger" | "Warning" | "Success">);
10const severities = severitiesType();
pineafanad54d752022-04-18 19:01:43 +010011
Skyler Grey75ea9172022-08-06 10:22:23 +010012export default async function (
13 type: string,
14 guild: string,
15 message: string | true,
PineaFan1dee28f2023-01-16 22:09:07 +000016 severity: "Critical" | "Warning" | "Info" = "Info",
17 pings?: string[]
Skyler Grey75ea9172022-08-06 10:22:23 +010018) {
pineafan63fc5e22022-08-04 22:04:10 +010019 const data = await client.database.guilds.read(guild);
PineaFan100df682023-01-02 13:26:08 +000020 if (data.logging.staff.channel === null) return;
pineafane23c4ec2022-07-27 21:56:27 +010021 if (message === true) {
Skyler Grey75ea9172022-08-06 10:22:23 +010022 return await client.database.guilds.write(guild, {
23 [`singleEventNotifications.${type}`]: false
24 });
pineafane23c4ec2022-07-27 21:56:27 +010025 }
pineafan6702cef2022-06-13 17:52:37 +010026 if (data.singleEventNotifications[type]) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010027 await client.database.guilds.write(guild, {
28 [`singleEventNotifications.${type}`]: true
29 });
pineafanad54d752022-04-18 19:01:43 +010030 try {
pineafan63fc5e22022-08-04 22:04:10 +010031 const channel = await client.channels.fetch(data.logging.staff.channel);
pineafan6702cef2022-06-13 17:52:37 +010032 if (!channel) return;
PineaFan100df682023-01-02 13:26:08 +000033 if (!channel.isTextBased()) return;
PineaFan1dee28f2023-01-16 22:09:07 +000034 if (pings) {
35 await channel.send({
36 content: pings.map((ping) => `<@${ping}>`).join(" ")
37 });
38 }
Skyler Grey75ea9172022-08-06 10:22:23 +010039 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 });
pineafanad54d752022-04-18 19:01:43 +010048 } catch (err) {
pineafan63fc5e22022-08-04 22:04:10 +010049 console.error(err);
pineafanad54d752022-04-18 19:01:43 +010050 }
51}