Adds some more polish

This commit is contained in:
Michel Fedde 2025-06-26 21:38:03 +02:00
parent b3d0b3a90c
commit 11bd836ec3
18 changed files with 272 additions and 29 deletions

View file

@ -7,6 +7,9 @@ import {PlaydateModel} from "../Database/Models/PlaydateModel";
import {TimedEvent} from "./EventHandler.types";
import {CleanupEvent} from "./Handlers/CleanupEvent";
import {Logger} from "log4js";
import {ElementChangedEvent} from "./EventClasses/ElementChangedEvent";
import {GroupModel} from "../Database/Models/GroupModel";
import {sendLeaderChangeNotificationEventHandler} from "./Handlers/LeaderChanged";
export class DefaultEvents {
public static setupTimed() {
@ -29,5 +32,9 @@ export class DefaultEvents {
method: sendCreatedNotificationEventHandler,
persistent: true
});
eventHandler.addHandler<ElementChangedEvent<GroupModel>>(ElementChangedEvent.name, {
method: sendLeaderChangeNotificationEventHandler,
persistent: true
})
}
}

View file

@ -0,0 +1,13 @@
import {Model} from "../../Database/Models/Model";
import {EventType, NormalEvent} from "../EventHandler.types";
import {DeepPartial} from "../../types/Partial";
export class ElementChangedEvent<T extends Model = Model> implements NormalEvent {
constructor(
public readonly tableName: string,
public readonly changes: DeepPartial<T> & Model,
) {
}
type: EventType.Normal = EventType.Normal;
}

View file

@ -0,0 +1,81 @@
import {ElementChangedEvent} from "../EventClasses/ElementChangedEvent";
import {GroupModel} from "../../Database/Models/GroupModel";
import Groups from "../../Database/tables/Groups";
import {Container} from "../../Container/Container";
import {GroupRepository} from "../../Database/Repositories/GroupRepository";
import {ConfigurationHandler} from "../../Configuration/ConfigurationHandler";
import {GroupConfigurationModel} from "../../Database/Models/GroupConfigurationModel";
import {
GroupConfigurationProvider,
RuntimeGroupConfiguration
} from "../../Configuration/Groups/GroupConfigurationProvider";
import {GroupConfigurationRepository} from "../../Database/Repositories/GroupConfigurationRepository";
import {DiscordClient} from "../../Discord/DiscordClient";
import {EmbedLibrary} from "../../Discord/EmbedLibrary";
import {roleMention, userMention} from "discord.js";
import * as util from "node:util";
import {ArrayUtils} from "../../Utilities/ArrayUtils";
const CHANGED_LINES = [
"Look who now manages your group, its %s. He will do a fantastic job!",
"Oh the 14th god changed... again, now its %s",
"This group was given to %s",
"This world was given over to %s, lets hope you survive the next adventure :smiling_imp:"
]
export async function sendLeaderChangeNotificationEventHandler(event: ElementChangedEvent<GroupModel>) {
if (event.tableName !== Groups.name) {
return;
}
if (!event.changes.leader?.memberid) {
return;
}
const group = Container.get<GroupRepository>(GroupRepository.name).getById(event.changes.id);
if (!group) {
return;
}
const groupConfig = new ConfigurationHandler<GroupConfigurationModel, RuntimeGroupConfiguration>(
new GroupConfigurationProvider(
Container.get<GroupConfigurationRepository>(GroupConfigurationRepository.name),
group
)
);
const targetChannel = groupConfig.getConfigurationByPath('channels.notifications').value;
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 = EmbedLibrary.withGroup(
group,
"You have a new leader",
util.format(ArrayUtils.chooseRandom(CHANGED_LINES), userMention(group.leader.memberid))
)
channel.send({
content: roleMention(group.role.roleid),
embeds: [
embed
],
allowedMentions: {
roles: [group.role.roleid]
}
})
}

View file

@ -1,7 +1,7 @@
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 {EmbedBuilder, roleMention, time, userMention} from "discord.js";
import {ArrayUtils} from "../../Utilities/ArrayUtils";
import {Container} from "../../Container/Container";
import {GroupConfigurationRepository} from "../../Database/Repositories/GroupConfigurationRepository";
@ -12,6 +12,7 @@ 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. >:]',
@ -37,7 +38,7 @@ export async function sendCreatedNotificationEventHandler(event: ElementCreatedE
)
);
const targetChannel = groupConfig.getConfigurationByPath('channels.newPlaydates').value;
const targetChannel = groupConfig.getConfigurationByPath('channels.notifications').value;
if (!targetChannel) {
return;
}
@ -55,17 +56,14 @@ export async function sendCreatedNotificationEventHandler(event: ElementCreatedE
return;
}
const embed = new EmbedBuilder()
.setTitle("New Playdate added")
.setDescription(
ArrayUtils.chooseRandom(NEW_PLAYDATE_MESSAGES)
)
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')}`,
})
.setFooter({
text: `Group: ${playdate.group.name}`
});
channel.send({