feat(configuration): Adds missing implermentations

This commit is contained in:
Michel Fedde 2025-06-22 16:27:34 +02:00
parent b82ab7dbc4
commit ec0aa5654c
7 changed files with 150 additions and 32 deletions

View file

@ -2,7 +2,15 @@ import {
SlashCommandBuilder,
CommandInteraction,
AutocompleteInteraction,
EmbedBuilder, MessageFlags, ChatInputCommandInteraction, time, AttachmentBuilder, ActivityFlagsBitField, Options
EmbedBuilder,
MessageFlags,
ChatInputCommandInteraction,
time,
AttachmentBuilder,
ActivityFlagsBitField,
Options,
User,
GuildMember
} from "discord.js";
import {AutocompleteCommand, ChatInteractionCommand, Command} from "./Command";
import {Container} from "../../Container/Container";
@ -13,6 +21,10 @@ import {PlaydateRepository} from "../../Repositories/PlaydateRepository";
import {GroupModel} from "../../Models/GroupModel";
import * as ics from 'ics';
import ical from 'node-ical';
import {GroupConfigurationHandler} from "../../Groups/GroupConfigurationHandler";
import {GroupConfigurationRepository} from "../../Repositories/GroupConfigurationRepository";
import {privateDecrypt} from "node:crypto";
import {GroupRepository} from "../../Repositories/GroupRepository";
export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInteractionCommand {
definition(): SlashCommandBuilder {
@ -100,6 +112,13 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
}
async create(interaction: CommandInteraction, group: GroupModel): Promise<void> {
if (!this.interactionIsAllowedToManage(<ChatInputCommandInteraction>interaction, group)) {
throw new UserError(
"You are not allowed to create playdates for this group.",
"Ask your Game Master to add the playdate or ask him to allow everyone to do so."
)
}
const fromDate = Date.parse(<string>interaction.options.get("from")?.value ?? '');
const toDate = Date.parse(<string>interaction.options.get("to")?.value ?? '');
@ -199,6 +218,13 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
}
private async delete(interaction: ChatInputCommandInteraction, group: GroupModel): Promise<void> {
if (!this.interactionIsAllowedToManage(<ChatInputCommandInteraction>interaction, group)) {
throw new UserError(
"You are not allowed to delete playdates for this group.",
"Ask your Game Master to delete the playdate or ask him to allow everyone to do so."
)
}
const playdateId = interaction.options.getInteger("playdate", true)
const repo = Container.get<PlaydateRepository>(PlaydateRepository.name);
@ -231,6 +257,13 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
}
private async import(interaction: ChatInputCommandInteraction, group: GroupModel): Promise<void> {
if (!this.interactionIsAllowedToManage(<ChatInputCommandInteraction>interaction, group)) {
throw new UserError(
"You are not allowed to create playdates for this group.",
"Ask your Game Master to add the playdate or ask him to allow everyone to do so."
)
}
const file = interaction.options.getAttachment('file', true);
const mimeType = file.contentType?.split(';')[0];
if (mimeType !== "text/calendar") {
@ -281,8 +314,25 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
}
private async export(interaction: ChatInputCommandInteraction, group: GroupModel): Promise<void> {
const groupConfig = new GroupConfigurationHandler(
Container.get<GroupConfigurationRepository>(GroupConfigurationRepository.name),
group
).getConfiguration();
const playdates = this.getExportTargets(interaction, group);
if (playdates.length < 1) {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setTitle("Export failed")
.setDescription(`:x: No playdates found under those filters.`)
],
flags: MessageFlags.Ephemeral
});
return
}
const result = ics.createEvents(
playdates.map((playdate) => {
return {
@ -292,7 +342,9 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
end: ics.convertTimestampToArray(playdate.to_time.getTime(), ''),
endInputType: 'utc',
endOutputType: 'utc',
title: `PnP with ${group.name}`,
title: groupConfig.calendar.title ?? `PnP with ${group.name}`,
description: groupConfig.calendar.description ?? undefined,
location: groupConfig.calendar.location ?? undefined,
status: "CONFIRMED",
busyStatus: "FREE",
categories: ['PnP']
@ -332,4 +384,22 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
return [playdate];
}
private interactionIsAllowedToManage(interaction: ChatInputCommandInteraction, group: GroupModel): boolean {
const interactionMemberId = interaction.member?.user.id;
if (group.leader.memberid === interactionMemberId) {
return true;
}
const groupRepo = Container.get<GroupRepository>(GroupRepository.name);
if (!groupRepo.isMemberInGroup(<GuildMember>interaction.member, group)) {
return false;
}
const config = new GroupConfigurationHandler(
Container.get<GroupConfigurationRepository>(GroupConfigurationRepository.name),
group
);
return config.getConfiguration().permissions.allowMemberManagingPlaydates;
}
}

View file

@ -13,6 +13,7 @@ import {Container} from "../Container/Container";
import {EventHandler} from "../Events/EventHandler";
import {ModalInteractionEvent} from "../Events/EventClasses/ModalInteractionEvent";
import {ComponentInteractionEvent} from "../Events/EventClasses/ComponentInteractionEvent";
import {log} from "node:util";
enum InteractionRoutingType {
Unrouted,
@ -91,9 +92,9 @@ export class InteractionRouter {
await command.execute?.call(command, interaction);
} catch (e: any) {
this.logger.error(e)
let userMessage = ":x: There was an error while executing this command!";
let logErrorMessage = true;
if (e.constructor.name === UserError.name) {
userMessage = `:x: \`${e.message}\` - Please validate your request!`
if (e.tryInstead) {
@ -102,7 +103,14 @@ export class InteractionRouter {
You can try the following:
${inlineCode(e.tryInstead)}`
}
logErrorMessage = false;
}
if (logErrorMessage) {
this.logger.error(e)
}
if (interaction.replied || interaction.deferred) {
await interaction.followUp({content: userMessage, flags: MessageFlags.Ephemeral});
} else {