blob: 08c7af510b5f8ca55df5ed92e29750de77ee828f [file] [log] [blame]
import client from "./client.js";
import EmojiEmbed from "./generateEmojiEmbed.js";
import { Record as ImmutableRecord } from "immutable";
import type { TextChannel, ThreadChannel, NewsChannel } from "discord.js";
const severitiesType = ImmutableRecord({
Critical: "Danger",
Warning: "Warning",
Info: "Success"
} as Record<string, "Danger" | "Warning" | "Success">);
const severities = severitiesType();
export default async function (
type: string,
guild: string,
message: string | true,
severity: "Critical" | "Warning" | "Info" = "Info",
pings?: string[]
) {
const data = await client.database.guilds.read(guild);
if (data.logging.staff.channel === null) return;
if (message === true) {
return await client.database.guilds.write(guild, {
[`singleEventNotifications.${type}`]: false
});
}
if (data.singleEventNotifications[type]) return;
await client.database.guilds.write(guild, {
[`singleEventNotifications.${type}`]: true
});
try {
const channel = await client.channels.fetch(data.logging.staff.channel);
if (!channel) return;
if (!channel.isTextBased()) return;
const textChannel = channel as TextChannel | ThreadChannel | NewsChannel;
let messageData = {
embeds: [
new EmojiEmbed()
.setTitle(`${severity} notification`)
.setDescription(message)
.setStatus(severities.get(severity))
.setEmoji("CONTROL.BLOCKCROSS")
]
};
if (pings) {
messageData = Object.assign(messageData, {
content: pings.map((ping) => `<@${ping}>`).join(" ")
});
}
await textChannel.send(messageData);
} catch (err) {
console.error(err);
}
}