Adds some more polish
This commit is contained in:
parent
b3d0b3a90c
commit
11bd836ec3
18 changed files with 272 additions and 29 deletions
81
source/Events/Handlers/LeaderChanged.ts
Normal file
81
source/Events/Handlers/LeaderChanged.ts
Normal 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]
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue