Adds Event system and automatic messages
This commit is contained in:
parent
0e10ea3cab
commit
2f826fbf36
20 changed files with 428 additions and 18 deletions
75
source/Events/Handlers/SendCreatedNotification.ts
Normal file
75
source/Events/Handlers/SendCreatedNotification.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import {ElementCreatedEvent} from "../ElementCreatedEvent";
|
||||
import {DefaultHandler} from "../DefaultEvents";
|
||||
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 {GroupConfigurationRenderer} from "../../Groups/GroupConfigurationRenderer";
|
||||
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<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(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 ]
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue