From 1cae5619ab6b7b35ea9328bd22b01c8cf15bd792 Mon Sep 17 00:00:00 2001 From: newracket Date: Wed, 31 Jan 2024 00:51:44 -0800 Subject: [PATCH 1/4] Add date option to Checkin command --- src/commands/Checkin.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/commands/Checkin.ts b/src/commands/Checkin.ts index fb774d7..b6b050c 100644 --- a/src/commands/Checkin.ts +++ b/src/commands/Checkin.ts @@ -29,6 +29,9 @@ export default class Checkin extends Command { .addBooleanOption(option => option.setName('widescreen').setDescription('Include a slide for the QR code.').setRequired(false) ) + .addStringOption(option => + option.setName('date').setDescription('The date to check for events.').setRequired(false) + ) .setDescription( "Sends a DM or embed with all check-in codes from today's events. Includes Express Checkin QR code!" ); @@ -68,6 +71,15 @@ export default class Checkin extends Command { // Get arguments. Get rid of the null types by checking them. const publicArgument = interaction.options.getBoolean('public'); const widescreenArgument = interaction.options.getBoolean('widescreen'); + const dateArgument = interaction.options.getString('date'); + + // Regex to match dates in the format of MM/DD(/YYYY) or MM-DD(-YYYY). + const regexp = new RegExp('^(\\d{1,2})(/|-)(\\d{1,2})((/|-)(\\d{2}|\\d{4})){0,1}$', 'g'); + const dateMatches = regexp.exec(dateArgument!); + + const month = dateMatches?.[1] ? parseInt(dateMatches[1], 10) : DateTime.now().month; + const day = dateMatches?.[3] ? parseInt(dateMatches[3], 10) : DateTime.now().day; + const year = dateMatches?.[6] ? parseInt(dateMatches[6], 10) : DateTime.now().year; // By default, we want the QR code to be DMed to the user. const isPublic = publicArgument !== null ? publicArgument : false; @@ -87,22 +99,10 @@ export default class Checkin extends Command { // We need an array to store all events that have a start time within today's timeframe. const todayEvents = futureEvents.filter(event => { // get today's midnight - const midnightToday = DateTime.now().set({ - hour: 0, - minute: 0, - second: 0, - millisecond: 0, - }); + const midnightToday = DateTime.local(year, month, day, 0, 0, 0, 0); // get tomorrow's midnight - const midnightTomorrow = DateTime.now() - .set({ - hour: 0, - minute: 0, - second: 0, - millisecond: 0, - }) - .plus({ day: 1 }); + const midnightTomorrow = midnightToday.plus({ day: 1 }); // check if start time in between return Interval.fromDateTimes(midnightToday, midnightTomorrow).contains(event.start); From e0aa0d0e030cabac34b67f5f34c70ca2aeb9162d Mon Sep 17 00:00:00 2001 From: newracket Date: Wed, 31 Jan 2024 00:53:04 -0800 Subject: [PATCH 2/4] Update date description in Checkin command --- src/commands/Checkin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/Checkin.ts b/src/commands/Checkin.ts index b6b050c..80dba15 100644 --- a/src/commands/Checkin.ts +++ b/src/commands/Checkin.ts @@ -30,7 +30,7 @@ export default class Checkin extends Command { option.setName('widescreen').setDescription('Include a slide for the QR code.').setRequired(false) ) .addStringOption(option => - option.setName('date').setDescription('The date to check for events.').setRequired(false) + option.setName('date').setDescription('The date to check for events. Use MM/DD format.').setRequired(false) ) .setDescription( "Sends a DM or embed with all check-in codes from today's events. Includes Express Checkin QR code!" From 839b534ae493173ff794d387dc458b1251a32a90 Mon Sep 17 00:00:00 2001 From: newracket Date: Wed, 31 Jan 2024 16:51:45 -0800 Subject: [PATCH 3/4] Fix invalid date format handling in Checkin command --- src/commands/Checkin.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/commands/Checkin.ts b/src/commands/Checkin.ts index 80dba15..19051a9 100644 --- a/src/commands/Checkin.ts +++ b/src/commands/Checkin.ts @@ -77,6 +77,14 @@ export default class Checkin extends Command { const regexp = new RegExp('^(\\d{1,2})(/|-)(\\d{1,2})((/|-)(\\d{2}|\\d{4})){0,1}$', 'g'); const dateMatches = regexp.exec(dateArgument!); + if (dateArgument !== null && dateMatches === null) { + await super.respond(interaction, { + content: 'Invalid date format. Please use MM/DD or MM-DD format.', + ephemeral: true, + }); + return; + } + const month = dateMatches?.[1] ? parseInt(dateMatches[1], 10) : DateTime.now().month; const day = dateMatches?.[3] ? parseInt(dateMatches[3], 10) : DateTime.now().day; const year = dateMatches?.[6] ? parseInt(dateMatches[6], 10) : DateTime.now().year; From 3b0ae174caf0047729927555cc0dee2e610e8b6d Mon Sep 17 00:00:00 2001 From: newracket Date: Wed, 14 Feb 2024 16:48:07 -0800 Subject: [PATCH 4/4] Fix date validation issue --- src/commands/Checkin.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/commands/Checkin.ts b/src/commands/Checkin.ts index 19051a9..45d08c9 100644 --- a/src/commands/Checkin.ts +++ b/src/commands/Checkin.ts @@ -89,6 +89,14 @@ export default class Checkin extends Command { const day = dateMatches?.[3] ? parseInt(dateMatches[3], 10) : DateTime.now().day; const year = dateMatches?.[6] ? parseInt(dateMatches[6], 10) : DateTime.now().year; + if (month < 1 || month > 12 || day < 1 || day > 31) { + await super.respond(interaction, { + content: 'Invalid date. Please use a valid date.', + ephemeral: true, + }); + return; + } + // By default, we want the QR code to be DMed to the user. const isPublic = publicArgument !== null ? publicArgument : false; // By default, we want to include the slide.