import {ElementCreatedEvent} from "../EventClasses/ElementCreatedEvent"; import {PlaydateModel} from "../../Database/Models/PlaydateModel"; import PlaydateTableConfiguration from "../../Database/tables/Playdate"; import {EmbedBuilder, roleMention, time, userMention} from "discord.js"; import {ArrayUtils} from "../../Utilities/ArrayUtils"; import {Container} from "../../Container/Container"; import {GroupConfigurationRepository} from "../../Database/Repositories/GroupConfigurationRepository"; import {DiscordClient} from "../../Discord/DiscordClient"; import {ConfigurationHandler} from "../../Configuration/ConfigurationHandler"; import {GroupConfigurationModel} from "../../Database/Models/GroupConfigurationModel"; import { GroupConfigurationProvider, RuntimeGroupConfiguration } from "../../Configuration/Groups/GroupConfigurationProvider"; import {EmbedLibrary} from "../../Discord/EmbedLibrary"; const NEW_PLAYDATE_MESSAGES = [ 'A new playdate was added. Lets hope, your GM has not planned to kill you. >:]', 'Oh look. A new playdate... neat.', 'A new playdate. Lets polish the dice.' ]; export async function sendCreatedNotificationEventHandler(event: ElementCreatedEvent) { if (event.tableName !== PlaydateTableConfiguration.name) { return; } const playdate = event.instanceValues; if (!playdate.group || !playdate.from_time || !playdate.to_time) { return; } const groupConfig = new ConfigurationHandler( new GroupConfigurationProvider( Container.get(GroupConfigurationRepository.name), playdate.group ) ); const targetChannel = groupConfig.getConfigurationByPath('channels.notifications').value; if (!targetChannel) { return; } const channel = await Container.get(DiscordClient.name).Client.channels.fetch(targetChannel) if (!channel) { return; } if (!channel.isTextBased()) { return; } if (!channel.isSendable()) { return; } const embed = EmbedLibrary.withGroup( playdate.group, "New Playdate added", ArrayUtils.chooseRandom(NEW_PLAYDATE_MESSAGES) ) .addFields({ name: "Playdate:", value: `${time(playdate.from_time, "F")} - ${time(playdate.to_time, 'F')}`, }); channel.send({ content: roleMention(playdate.group.role.roleid), embeds: [ embed ], allowedMentions: { roles: [playdate.group.role.roleid] } }) }