Adds Event system and automatic messages

This commit is contained in:
Michel Fedde 2025-05-25 16:07:09 +02:00
parent 0e10ea3cab
commit 2f826fbf36
20 changed files with 428 additions and 18 deletions

View file

@ -30,7 +30,7 @@ export class GroupSelection {
)
}
public static getGroup(interaction: CommandInteraction): GroupModel {
public static getGroup(interaction: CommandInteraction|AutocompleteInteraction): GroupModel {
const groupname = interaction.options.get("group", true);
if (!groupname) {
throw new UserError("No group name provided");

View file

@ -4,7 +4,7 @@ import {
CommandInteraction,
AutocompleteInteraction,
GuildMember,
EmbedBuilder, MessageFlags, ChatInputCommandInteraction, ModalSubmitFields
EmbedBuilder, MessageFlags, ChatInputCommandInteraction, ModalSubmitFields, time, User
} from "discord.js";
import {AutocompleteCommand, ChatInteractionCommand, Command} from "./Command";
import {Container} from "../../Container/Container";
@ -93,17 +93,32 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
throw new UserError("No date or invalid date format for the to parameter.");
}
if (fromDate > toDate) {
throw new UserError("The to-date can't be earlier than the from-date");
}
const playdateRepo = Container.get<PlaydateRepository>(PlaydateRepository.name);
const collidingTimes = playdateRepo.findPlaydatesInRange(fromDate, toDate, group);
if (collidingTimes.length > 0) {
throw new UserError("The playdate collides with another playdate. Please either remove the old one or choose a different time.")
}
const playdate: Partial<PlaydateModel> = {
group: group,
from_time: new Date(fromDate),
to_time: new Date(toDate),
}
const id = Container.get<PlaydateRepository>(PlaydateRepository.name).create(playdate);
const id = playdateRepo.create(playdate);
const embed = new EmbedBuilder()
.setTitle("Created a play-date.")
.setDescription(":white_check_mark: Your playdate has been created! You and your group get notified, when its time.")
.setFields({
name: "Created playdate",
value: `${time(new Date(fromDate),'F')} - ${time(new Date(toDate), 'F')}`,
})
.setFooter({
text: `Group: ${group.name}`
})
@ -127,12 +142,8 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
return;
}
const groupname = interaction.options.getString("group")
const group = Container.get<GroupRepository>(GroupRepository.name).findGroupByName((groupname ?? '').toString());
if (!group) {
throw new UserError("No group found");
}
const group = GroupSelection.getGroup(interaction);
const playdates = Container.get<PlaydateRepository>(PlaydateRepository.name).findFromGroup(group);
await interaction.respond(
playdates.map(playdate => {
@ -153,8 +164,8 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter
playdates.map((playdate) =>
{
return {
name: `${playdate.from_time.toLocaleString()} - ${playdate.to_time.toLocaleString()}`,
value: ``
name: `${time(playdate.from_time, 'F')} - ${time(playdate.to_time, 'F')}`,
value: `${time(playdate.from_time, 'R')}`
}
})
)