From 1c7a97b616b44447fa56cedece8b022b1de6201a Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Sat, 29 Apr 2023 01:50:01 -0400 Subject: [PATCH] Add DateTime valid check --- src/buttons/event-creation/dateTimeUtils.ts | 3 ++- src/buttons/event-creation/step1-gameSelection.ts | 14 ++++++++------ src/buttons/event-creation/step2-finalize.ts | 3 ++- src/buttons/event-creation/utils.ts | 6 ++++-- src/buttons/live-event/applyDateTime.ts | 12 ++++++------ 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/buttons/event-creation/dateTimeUtils.ts b/src/buttons/event-creation/dateTimeUtils.ts index 4d37648..a1047ff 100644 --- a/src/buttons/event-creation/dateTimeUtils.ts +++ b/src/buttons/event-creation/dateTimeUtils.ts @@ -154,7 +154,7 @@ const parseEventDate = (preParsedEventDate: string): [string, string, string] => }; // Take full raw Date/Time input and convert it to a proper Date -export const getDateFromRawInput = (rawEventTime: string, rawEventTimeZone: string, rawEventDate: string): [Date, string, boolean] => { +export const getDateFromRawInput = (rawEventTime: string, rawEventTimeZone: string, rawEventDate: string): [Date, string, boolean, boolean] => { // Verify/Set Time const [parsedEventTimeHours, parsedEventTimeMinutes, parsedEventTimePeriod] = parseEventTime(rawEventTime.replaceAll(':', '').toUpperCase()); @@ -171,5 +171,6 @@ export const getDateFromRawInput = (rawEventTime: string, rawEventTimeZone: stri parsedEventMonth.slice(1, 3).toLowerCase() } ${parsedEventDay}, ${parsedEventYear}`, parsedDateTime.getTime() > new Date().getTime(), + !isNaN(parsedDateTime.getTime()), ]; }; diff --git a/src/buttons/event-creation/step1-gameSelection.ts b/src/buttons/event-creation/step1-gameSelection.ts index f9e27ca..610db82 100644 --- a/src/buttons/event-creation/step1-gameSelection.ts +++ b/src/buttons/event-creation/step1-gameSelection.ts @@ -2,7 +2,7 @@ import { ActionRow, ApplicationCommandFlags, ApplicationCommandTypes, Bot, Butto import { infoColor1, somethingWentWrong } from '../../commandUtils.ts'; import { CommandDetails } from '../../types/commandTypes.ts'; import { Activities } from './activities.ts'; -import { generateActionRow, getNestedActivity } from './utils.ts'; +import { generateActionRow, getNestedActivity, invalidDateTimeStr } from './utils.ts'; import { dateTimeFields, descriptionTextField, fillerChar, idSeparator, LfgEmbedIndexes, lfgStartTimeName, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts'; import { addTokenToMap, deleteTokenEarly, generateMapId, selfDestructMessage, tokenMap } from '../tokenCleanup.ts'; import utils from '../../utils.ts'; @@ -53,11 +53,13 @@ const execute = async (bot: Bot, interaction: Interaction) => { let prefillDate = ''; let prefillDescription = ''; if (interaction.message && interaction.message.embeds[0].fields && interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].name === lfgStartTimeName) { - let rawEventDateTime = interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value.split('\n')[0].split(' '); - const monthIdx = rawEventDateTime.findIndex((item) => monthsShort.includes(item.toUpperCase())); - prefillTime = rawEventDateTime.slice(0, monthIdx - 1).join(' ').trim(); - prefillTimeZone = rawEventDateTime[monthIdx - 1].trim(); - prefillDate = rawEventDateTime.slice(monthIdx).join(' ').trim(); + if (interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value !== invalidDateTimeStr) { + let rawEventDateTime = interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value.split('\n')[0].split(' '); + const monthIdx = rawEventDateTime.findIndex((item) => monthsShort.includes(item.toUpperCase())); + prefillTime = rawEventDateTime.slice(0, monthIdx - 1).join(' ').trim(); + prefillTimeZone = (rawEventDateTime[monthIdx - 1] || '').trim(); + prefillDate = rawEventDateTime.slice(monthIdx).join(' ').trim(); + } prefillDescription = interaction.message.embeds[0].fields[LfgEmbedIndexes.Description].value.trim(); } diff --git a/src/buttons/event-creation/step2-finalize.ts b/src/buttons/event-creation/step2-finalize.ts index 33dd781..667014c 100644 --- a/src/buttons/event-creation/step2-finalize.ts +++ b/src/buttons/event-creation/step2-finalize.ts @@ -53,7 +53,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { } // Get Date Object from user input - const [eventDateTime, eventDateTimeStr, eventInFuture] = getDateFromRawInput(rawEventTime, rawEventTimeZone, rawEventDate); // TODO: verify dt + const [eventDateTime, eventDateTimeStr, eventInFuture, dateTimeValid] = getDateFromRawInput(rawEventTime, rawEventTimeZone, rawEventDate); addTokenToMap(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id); bot.helpers.sendInteractionResponse( @@ -74,6 +74,7 @@ const execute = async (bot: Bot, interaction: Interaction) => { [], customIdIdxPath, eventInFuture, + dateTimeValid, ), ).catch((e: Error) => utils.commonLoggers.interactionSendError('step2-finalize.ts', interaction, e)); } else { diff --git a/src/buttons/event-creation/utils.ts b/src/buttons/event-creation/utils.ts index 43c71a6..534a58d 100644 --- a/src/buttons/event-creation/utils.ts +++ b/src/buttons/event-creation/utils.ts @@ -56,6 +56,7 @@ export const generateActionRow = (baseValue: string, activities: Array const createEventBtnName = 'Create Event'; const createWhitelistedBtnName = 'Create Whitelisted Event'; const editEventDetailsBtnName = 'Edit Event Details'; +export const invalidDateTimeStr = '`Invalid Date/Time`'; const finalizeButtons = (idxPath: string, eventInFuture: boolean): [ButtonComponent, ButtonComponent, ButtonComponent] | [ButtonComponent] => { const editButton: ButtonComponent = { type: MessageComponentTypes.Button, @@ -127,6 +128,7 @@ export const createLFGPost = ( alternateList: Array, idxPath: string, eventInFuture: boolean, + dateTimeValid: boolean, ): InteractionResponse => { const icsDetails = `${category}: ${activity.name}`; return { @@ -136,7 +138,7 @@ export const createLFGPost = ( content: eventInFuture ? `Please verify the information below, then click on the \`${createEventBtnName}\` or \`${createWhitelistedBtnName}\` button, or change the event \`Date/Time\` or \`Description\` with the \`${editEventDetailsBtnName}\` button below. \n\n${selfDestructMessage(new Date().getTime()) }` - : `You cannot create an event in the past. Please change the event's \`Date/Time\` to be in the future with the \`${editEventDetailsBtnName}\` button below.`, + : `You cannot create an event ${dateTimeValid ? 'in the past' : 'with an invalid date/time'}. Please change the event's \`Date/Time\` to be ${dateTimeValid ? 'in the future' : 'valid'} with the \`${editEventDetailsBtnName}\` button below.`, embeds: [{ color: eventInFuture ? successColor : warnColor, fields: [{ @@ -145,7 +147,7 @@ export const createLFGPost = ( inline: true, }, { name: lfgStartTimeName, - value: generateTimeFieldStr(eventDateTimeStr, eventDateTime), + value: dateTimeValid ? generateTimeFieldStr(eventDateTimeStr, eventDateTime) : invalidDateTimeStr, inline: true, }, { name: 'Add to Calendar:', diff --git a/src/buttons/live-event/applyDateTime.ts b/src/buttons/live-event/applyDateTime.ts index 8d866ad..cab9361 100644 --- a/src/buttons/live-event/applyDateTime.ts +++ b/src/buttons/live-event/applyDateTime.ts @@ -32,20 +32,20 @@ const execute = async (bot: Bot, interaction: Interaction) => { } // Get Date Object from user input - const [eventDateTime, eventDateTimeStr, eventInFuture] = getDateFromRawInput(newTime, newTimeZone, newDate); // TODO: verify dt - if (!eventInFuture) { + const [eventDateTime, eventDateTimeStr, eventInFuture, dateTimeValid] = getDateFromRawInput(newTime, newTimeZone, newDate); + if (!eventInFuture || !dateTimeValid) { bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { type: InteractionResponseTypes.ChannelMessageWithSource, data: { flags: ApplicationCommandFlags.Ephemeral, embeds: [{ color: warnColor, - title: 'You cannot create an event in the past.', - description: 'Please dismiss this message and try again with a date in the future', - fields: [{ + title: dateTimeValid ? 'You cannot create an event in the past.' : 'Could not parse date/time.', + description: `Please dismiss this message and try again with a ${dateTimeValid ? 'date in the future' : 'valid date/time'}.`, + fields: dateTimeValid ? [{ name: 'Date/Time Entered:', value: generateTimeFieldStr(eventDateTimeStr, eventDateTime), - }], + }] : undefined, }], }, }).catch((e: Error) => utils.commonLoggers.interactionSendError('applyDateTime.ts', interaction, e));