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) { if (event.tableName !== Groups.name) { return; } if (!event.changes.leader?.memberid) { return; } const group = Container.get(GroupRepository.name).getById(event.changes.id); if (!group) { return; } const groupConfig = new ConfigurationHandler( new GroupConfigurationProvider( Container.get(GroupConfigurationRepository.name), 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( 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] } }) }