import {ElementCreatedEvent} from "../EventClasses/ElementCreatedEvent"; import {PlaydateModel} from "../../Models/PlaydateModel"; import PlaydateTableConfiguration from "../../Database/tables/Playdate"; import {EmbedBuilder, roleMention, time} from "discord.js"; import {ArrayUtils} from "../../Utilities/ArrayUtils"; import {GroupConfigurationHandler} from "../../Groups/GroupConfigurationHandler"; import {Container} from "../../Container/Container"; import {GroupConfigurationRepository} from "../../Repositories/GroupConfigurationRepository"; import {DiscordClient} from "../../Discord/DiscordClient"; 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 configurationHandler = new GroupConfigurationHandler( Container.get(GroupConfigurationRepository.name), playdate.group ); const targetChannel = configurationHandler.getConfigurationByPath('channels.newPlaydates'); 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 = new EmbedBuilder() .setTitle("New Playdate added") .setDescription( ArrayUtils.chooseRandom(NEW_PLAYDATE_MESSAGES) ) .addFields({ name: "Playdate:", value: `${time(playdate.from_time, "F")} - ${time(playdate.to_time, 'F')}`, }) .setFooter({ text: `Group: ${playdate.group.name}` }); channel.send({ content: roleMention(playdate.group.role.roleid), embeds: [ embed ], allowedMentions: { roles: [playdate.group.role.roleid] } }) }