blob: 08c7af510b5f8ca55df5ed92e29750de77ee828f [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";
pineafan96228bd2023-02-21 14:22:55 +00004import type { TextChannel, ThreadChannel, NewsChannel } from "discord.js";
pineafanad54d752022-04-18 19:01:43 +01005
pineafanbd02b4a2022-08-05 22:01:38 +01006const severitiesType = ImmutableRecord({
Skyler Grey75ea9172022-08-06 10:22:23 +01007 Critical: "Danger",
8 Warning: "Warning",
9 Info: "Success"
pineafanbd02b4a2022-08-05 22:01:38 +010010} as Record<string, "Danger" | "Warning" | "Success">);
11const severities = severitiesType();
pineafanad54d752022-04-18 19:01:43 +010012
Skyler Grey75ea9172022-08-06 10:22:23 +010013export default async function (
14 type: string,
15 guild: string,
16 message: string | true,
PineaFan1dee28f2023-01-16 22:09:07 +000017 severity: "Critical" | "Warning" | "Info" = "Info",
18 pings?: string[]
Skyler Grey75ea9172022-08-06 10:22:23 +010019) {
pineafan63fc5e22022-08-04 22:04:10 +010020 const data = await client.database.guilds.read(guild);
PineaFan100df682023-01-02 13:26:08 +000021 if (data.logging.staff.channel === null) return;
pineafane23c4ec2022-07-27 21:56:27 +010022 if (message === true) {
Skyler Grey75ea9172022-08-06 10:22:23 +010023 return await client.database.guilds.write(guild, {
24 [`singleEventNotifications.${type}`]: false
25 });
pineafane23c4ec2022-07-27 21:56:27 +010026 }
pineafan6702cef2022-06-13 17:52:37 +010027 if (data.singleEventNotifications[type]) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010028 await client.database.guilds.write(guild, {
29 [`singleEventNotifications.${type}`]: true
30 });
pineafanad54d752022-04-18 19:01:43 +010031 try {
pineafan63fc5e22022-08-04 22:04:10 +010032 const channel = await client.channels.fetch(data.logging.staff.channel);
pineafan6702cef2022-06-13 17:52:37 +010033 if (!channel) return;
PineaFan100df682023-01-02 13:26:08 +000034 if (!channel.isTextBased()) return;
pineafan96228bd2023-02-21 14:22:55 +000035 const textChannel = channel as TextChannel | ThreadChannel | NewsChannel;
Skyler Greyda16adf2023-03-05 10:22:12 +000036 let messageData = {
37 embeds: [
38 new EmojiEmbed()
39 .setTitle(`${severity} notification`)
40 .setDescription(message)
41 .setStatus(severities.get(severity))
42 .setEmoji("CONTROL.BLOCKCROSS")
43 ]
44 };
PineaFan1dee28f2023-01-16 22:09:07 +000045 if (pings) {
pineafan96228bd2023-02-21 14:22:55 +000046 messageData = Object.assign(messageData, {
PineaFan1dee28f2023-01-16 22:09:07 +000047 content: pings.map((ping) => `<@${ping}>`).join(" ")
48 });
49 }
pineafan96228bd2023-02-21 14:22:55 +000050 await textChannel.send(messageData);
pineafanad54d752022-04-18 19:01:43 +010051 } catch (err) {
pineafan63fc5e22022-08-04 22:04:10 +010052 console.error(err);
pineafanad54d752022-04-18 19:01:43 +010053 }
54}