blob: 6bf63e193b9d12a6c1a9748b1da7ebffd122d677 [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;
36 let messageData = {embeds: [
37 new EmojiEmbed()
38 .setTitle(`${severity} notification`)
39 .setDescription(message)
40 .setStatus(severities.get(severity))
41 .setEmoji("CONTROL.BLOCKCROSS")
42 ]}
PineaFan1dee28f2023-01-16 22:09:07 +000043 if (pings) {
pineafan96228bd2023-02-21 14:22:55 +000044 messageData = Object.assign(messageData, {
PineaFan1dee28f2023-01-16 22:09:07 +000045 content: pings.map((ping) => `<@${ping}>`).join(" ")
46 });
47 }
pineafan96228bd2023-02-21 14:22:55 +000048 await textChannel.send(messageData);
pineafanad54d752022-04-18 19:01:43 +010049 } catch (err) {
pineafan63fc5e22022-08-04 22:04:10 +010050 console.error(err);
pineafanad54d752022-04-18 19:01:43 +010051 }
52}