pnp-scheduler/source/Events/Handlers/SendCreatedNotification.ts

73 lines
No EOL
2.2 KiB
TypeScript

import {ElementCreatedEvent} from "../EventClasses/ElementCreatedEvent";
import {PlaydateModel} from "../../Database/Models/PlaydateModel";
import PlaydateTableConfiguration from "../../Database/tables/Playdate";
import {EmbedBuilder, roleMention, time} from "discord.js";
import {ArrayUtils} from "../../Utilities/ArrayUtils";
import {GroupConfigurationHandler} from "../../Configuration/Groups/GroupConfigurationHandler";
import {Container} from "../../Container/Container";
import {GroupConfigurationRepository} from "../../Database/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<PlaydateModel>) {
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>(GroupConfigurationRepository.name),
playdate.group
);
const targetChannel = configurationHandler.getConfigurationByPath('channels.newPlaydates');
if (!targetChannel) {
return;
}
const channel = await Container.get<DiscordClient>(DiscordClient.name).Client.channels.fetch(<string>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]
}
})
}