diff --git a/source/Discord/Commands/Playdates.ts b/source/Discord/Commands/Playdates.ts index 7959084..a445d7c 100644 --- a/source/Discord/Commands/Playdates.ts +++ b/source/Discord/Commands/Playdates.ts @@ -4,7 +4,7 @@ import { ChatInputCommandInteraction, CommandInteraction, EmbedBuilder, - GuildMember, + GuildMember, hyperlink, MessageFlags, SlashCommandBuilder, time @@ -45,10 +45,12 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter .addStringOption((option) => option .setName("from") .setDescription("Defines the start date & time. Your desired format is probably support.") + .setRequired(true) ) .addStringOption((option) => option .setName("to") .setDescription("Defines the end date & time. Your desired format is probably support.") + .setRequired(true) ) ) .addSubcommand((subcommand) => subcommand @@ -130,20 +132,54 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter group ); + const fromDateString = interaction.options.get("from")?.value ?? ''; + const toDateString = interaction.options.get("to")?.value ?? ''; + + if (!this.checkDateString(fromDateString) || !this.checkDateString(toDateString)) { + throw new UserError( + "Please do not use words like 'Uhr', since those may result in values you did not intent.", + `Write the time out completely. Either in military time (15:00) or in the 12-hour format (3pm). For more valid formats check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}` + ) + } + const [fromDate, toDate] = timezoneHandler.use(() => { + const fromDate = parser.fromString(fromDateString); + const toAttempt = parser.attempt(toDateString); + + if (toAttempt.invalid) { + throw new UserError( + "No date or invalid date format for the to parameter.", + `If you want to make sure your format is valid, please check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}.` + ); + } + + const toDate = new Date(fromDate.getTime()); + if (toAttempt.year !== undefined) { + toDate.setFullYear(toAttempt.year, toAttempt.month ?? 0, toAttempt.day ?? 0); + } + if (toAttempt.hour !== undefined) { + toDate.setHours(toAttempt.hour, toAttempt.minute ?? 0, toAttempt.second ?? 0); + } + return [ - parser.fromString(interaction.options.get("from")?.value ?? ''), - parser.fromString(interaction.options.get("to")?.value ?? '') + fromDate, + toDate ] }) if (!fromDate.isValid()) { - throw new UserError("No date or invalid date format for the from parameter."); + throw new UserError( + "No date or invalid date format for the from parameter.", + `If you want to make sure your format is valid, please check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}.` + ); } if (!fromDate.isValid()) { - throw new UserError("No date or invalid date format for the to parameter."); + throw new UserError( + "No date or invalid date format for the to parameter.", + `If you want to make sure your format is valid, please check this ${hyperlink("list", "https://github.com/kensnyder/any-date-parser?tab=readme-ov-file#exhaustive-list-of-date-formats")}.` + ); } if (fromDate.getTime() > toDate.getTime()) { @@ -212,6 +248,12 @@ export class PlaydatesCommand implements Command, AutocompleteCommand, ChatInter }) ) } + + private checkDateString(value: string): boolean { + return [ + 'Uhr' + ].some((section) => value.includes(section)); + } private async list(interaction: ChatInputCommandInteraction, group: GroupModel) { const playdates = Container.get(PlaydateRepository.name).findFromGroup(group);