From 74c5c6e4e948b4c71e4434592935eaeb3c2b2cb8 Mon Sep 17 00:00:00 2001 From: Ean Milligan Date: Tue, 21 May 2024 03:52:32 -0400 Subject: [PATCH] Drastically improve the date parsing, allowing the user to do things group up doesn't recommend, such as using ISO8601, or going against the defaultDateFormat (at least when we can detect, if the user want 5/2/2025 instead of 2/5/2025, they need to follow the rules or spell it out) --- config.example.ts | 1 + src/buttons/event-creation/dateTimeUtils.ts | 42 ++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/config.example.ts b/config.example.ts index 8a9bc72..075a59b 100644 --- a/config.example.ts +++ b/config.example.ts @@ -18,6 +18,7 @@ export const config = {// !! NOTICE !! All fields below are required unless they 'addToCalendar': '', // Link to where the icsGenerator is hosted, OPTIONAL 'creatorIcon': '', // Link to where the GroupUpSinglePerson.png (or similar image) is hosted }, + 'defaultDateFormat': 'MONTH/DAY/YEAR', // Default format that Group Up will suggest to the user. Must match one of the options in the 'DateTimeFormats' enum inside 'src/buttons/event-creation/dateTimeUtils.ts' 'logChannel': 0n, // Discord channel ID where the bot should put startup messages and other error messages needed, OPTIONAL 'reportChannel': 0n, // Discord channel ID where reports will be sent when using the built-in report command, OPTIONAL 'devServer': 0n, // Discord guild ID where testing of indev features/commands will be handled, used in conjunction with the DEVMODE bool in mod.ts, OPTIONAL diff --git a/src/buttons/event-creation/dateTimeUtils.ts b/src/buttons/event-creation/dateTimeUtils.ts index 45bc427..ec276d6 100644 --- a/src/buttons/event-creation/dateTimeUtils.ts +++ b/src/buttons/event-creation/dateTimeUtils.ts @@ -1,6 +1,10 @@ import config from '../../../config.ts'; import {editEventDetailsBtnName} from './utils.ts'; +enum DateTimeFormats { + MMDDYYYY = 'MONTH/DAY/YEAR', + DDMMYYYY = 'DAY/MONTH/YEAR', +} const monthsLong: Array = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']; export const monthsShort: Array = monthsLong.map((month) => month.slice(0, 3)); const tzMap: Map = new Map([ @@ -146,7 +150,43 @@ const parseEventTimeZone = (preParsedEventTimeZone: string): [string, string] => // Takes user input Date and makes it actually usable const parseEventDate = (preParsedEventDate: string): [string, string, string] => { const today = new Date(); - let [parsedEventMonth, parsedEventDay, parsedEventYear] = preParsedEventDate.split(/[\s,\\/-]+/g); + let parsedEventMonth:string, parsedEventDay:string, parsedEventYear:string; + const [parsedSlot1, parsedSlot2, parsedSlot3] = preParsedEventDate.split(/[\s,\\/-]+/g); + + const slot1AsInt = parseInt(parsedSlot1); + const slot2AsInt = parseInt(parsedSlot2); + if (!isNaN(slot1AsInt) && slot1AsInt > 999) { + // First parsing slot appears to be a year, assume user used ISO8601 format + parsedEventYear = parsedSlot1; + parsedEventMonth = parsedSlot2; + parsedEventDay = parsedSlot3; + } else if (!isNaN(slot1AsInt) && slot1AsInt > 12) { + // First parsing slot appears to be a day, assume user used DDMMYYYY format + parsedEventDay = parsedSlot1; + parsedEventMonth = parsedSlot2; + parsedEventYear = parsedSlot3; + } else if (!isNaN(slot2AsInt) && slot2AsInt > 12) { + // Second parsing slot appears to be a day, assume user used MMDDYYYY format + parsedEventMonth = parsedSlot1; + parsedEventDay = parsedSlot2; + parsedEventYear = parsedSlot3; + } else { + // Year was not first, and cannot locate a day from the string, fall back to bot's default setting + if (config.defaultDateFormat === DateTimeFormats.DDMMYYYY) { + parsedEventDay = parsedSlot1; + parsedEventMonth = parsedSlot2; + parsedEventYear = parsedSlot3; + } else if (config.defaultDateFormat === DateTimeFormats.MMDDYYYY) { + parsedEventMonth = parsedSlot1; + parsedEventDay = parsedSlot2; + parsedEventYear = parsedSlot3; + } else { + // Worst case scenario and should not happen, but in case config.defaultDateFormat is screwed up, fall back to MMDDYYYY + parsedEventMonth = parsedSlot1; + parsedEventDay = parsedSlot2; + parsedEventYear = parsedSlot3; + } + } if (isNaN(parseInt(parsedEventDay))) { // User only provided one word, we're assuming it was TOMORROW, and all others will be treated as today