81 lines
No EOL
2.5 KiB
TypeScript
81 lines
No EOL
2.5 KiB
TypeScript
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]
|
|
}
|
|
})
|
|
} |