Add DateTime valid check

This commit is contained in:
Ean Milligan (Bastion) 2023-04-29 01:50:01 -04:00
parent 3f80b5ea63
commit 1c7a97b616
5 changed files with 22 additions and 16 deletions

View File

@ -154,7 +154,7 @@ const parseEventDate = (preParsedEventDate: string): [string, string, string] =>
}; };
// Take full raw Date/Time input and convert it to a proper Date // 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 // Verify/Set Time
const [parsedEventTimeHours, parsedEventTimeMinutes, parsedEventTimePeriod] = parseEventTime(rawEventTime.replaceAll(':', '').toUpperCase()); const [parsedEventTimeHours, parsedEventTimeMinutes, parsedEventTimePeriod] = parseEventTime(rawEventTime.replaceAll(':', '').toUpperCase());
@ -171,5 +171,6 @@ export const getDateFromRawInput = (rawEventTime: string, rawEventTimeZone: stri
parsedEventMonth.slice(1, 3).toLowerCase() parsedEventMonth.slice(1, 3).toLowerCase()
} ${parsedEventDay}, ${parsedEventYear}`, } ${parsedEventDay}, ${parsedEventYear}`,
parsedDateTime.getTime() > new Date().getTime(), parsedDateTime.getTime() > new Date().getTime(),
!isNaN(parsedDateTime.getTime()),
]; ];
}; };

View File

@ -2,7 +2,7 @@ import { ActionRow, ApplicationCommandFlags, ApplicationCommandTypes, Bot, Butto
import { infoColor1, somethingWentWrong } from '../../commandUtils.ts'; import { infoColor1, somethingWentWrong } from '../../commandUtils.ts';
import { CommandDetails } from '../../types/commandTypes.ts'; import { CommandDetails } from '../../types/commandTypes.ts';
import { Activities } from './activities.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 { dateTimeFields, descriptionTextField, fillerChar, idSeparator, LfgEmbedIndexes, lfgStartTimeName, pathIdxEnder, pathIdxSeparator } from '../eventUtils.ts';
import { addTokenToMap, deleteTokenEarly, generateMapId, selfDestructMessage, tokenMap } from '../tokenCleanup.ts'; import { addTokenToMap, deleteTokenEarly, generateMapId, selfDestructMessage, tokenMap } from '../tokenCleanup.ts';
import utils from '../../utils.ts'; import utils from '../../utils.ts';
@ -53,11 +53,13 @@ const execute = async (bot: Bot, interaction: Interaction) => {
let prefillDate = ''; let prefillDate = '';
let prefillDescription = ''; let prefillDescription = '';
if (interaction.message && interaction.message.embeds[0].fields && interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].name === lfgStartTimeName) { if (interaction.message && interaction.message.embeds[0].fields && interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].name === lfgStartTimeName) {
if (interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value !== invalidDateTimeStr) {
let rawEventDateTime = interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value.split('\n')[0].split(' '); let rawEventDateTime = interaction.message.embeds[0].fields[LfgEmbedIndexes.StartTime].value.split('\n')[0].split(' ');
const monthIdx = rawEventDateTime.findIndex((item) => monthsShort.includes(item.toUpperCase())); const monthIdx = rawEventDateTime.findIndex((item) => monthsShort.includes(item.toUpperCase()));
prefillTime = rawEventDateTime.slice(0, monthIdx - 1).join(' ').trim(); prefillTime = rawEventDateTime.slice(0, monthIdx - 1).join(' ').trim();
prefillTimeZone = rawEventDateTime[monthIdx - 1].trim(); prefillTimeZone = (rawEventDateTime[monthIdx - 1] || '').trim();
prefillDate = rawEventDateTime.slice(monthIdx).join(' ').trim(); prefillDate = rawEventDateTime.slice(monthIdx).join(' ').trim();
}
prefillDescription = interaction.message.embeds[0].fields[LfgEmbedIndexes.Description].value.trim(); prefillDescription = interaction.message.embeds[0].fields[LfgEmbedIndexes.Description].value.trim();
} }

View File

@ -53,7 +53,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
} }
// Get Date Object from user input // 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); addTokenToMap(bot, interaction, interaction.guildId, interaction.channelId, interaction.member.id);
bot.helpers.sendInteractionResponse( bot.helpers.sendInteractionResponse(
@ -74,6 +74,7 @@ const execute = async (bot: Bot, interaction: Interaction) => {
[], [],
customIdIdxPath, customIdIdxPath,
eventInFuture, eventInFuture,
dateTimeValid,
), ),
).catch((e: Error) => utils.commonLoggers.interactionSendError('step2-finalize.ts', interaction, e)); ).catch((e: Error) => utils.commonLoggers.interactionSendError('step2-finalize.ts', interaction, e));
} else { } else {

View File

@ -56,6 +56,7 @@ export const generateActionRow = (baseValue: string, activities: Array<Activity>
const createEventBtnName = 'Create Event'; const createEventBtnName = 'Create Event';
const createWhitelistedBtnName = 'Create Whitelisted Event'; const createWhitelistedBtnName = 'Create Whitelisted Event';
const editEventDetailsBtnName = 'Edit Event Details'; const editEventDetailsBtnName = 'Edit Event Details';
export const invalidDateTimeStr = '`Invalid Date/Time`';
const finalizeButtons = (idxPath: string, eventInFuture: boolean): [ButtonComponent, ButtonComponent, ButtonComponent] | [ButtonComponent] => { const finalizeButtons = (idxPath: string, eventInFuture: boolean): [ButtonComponent, ButtonComponent, ButtonComponent] | [ButtonComponent] => {
const editButton: ButtonComponent = { const editButton: ButtonComponent = {
type: MessageComponentTypes.Button, type: MessageComponentTypes.Button,
@ -127,6 +128,7 @@ export const createLFGPost = (
alternateList: Array<LFGMember>, alternateList: Array<LFGMember>,
idxPath: string, idxPath: string,
eventInFuture: boolean, eventInFuture: boolean,
dateTimeValid: boolean,
): InteractionResponse => { ): InteractionResponse => {
const icsDetails = `${category}: ${activity.name}`; const icsDetails = `${category}: ${activity.name}`;
return { return {
@ -136,7 +138,7 @@ export const createLFGPost = (
content: eventInFuture 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()) ? `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: [{ embeds: [{
color: eventInFuture ? successColor : warnColor, color: eventInFuture ? successColor : warnColor,
fields: [{ fields: [{
@ -145,7 +147,7 @@ export const createLFGPost = (
inline: true, inline: true,
}, { }, {
name: lfgStartTimeName, name: lfgStartTimeName,
value: generateTimeFieldStr(eventDateTimeStr, eventDateTime), value: dateTimeValid ? generateTimeFieldStr(eventDateTimeStr, eventDateTime) : invalidDateTimeStr,
inline: true, inline: true,
}, { }, {
name: 'Add to Calendar:', name: 'Add to Calendar:',

View File

@ -32,20 +32,20 @@ const execute = async (bot: Bot, interaction: Interaction) => {
} }
// Get Date Object from user input // Get Date Object from user input
const [eventDateTime, eventDateTimeStr, eventInFuture] = getDateFromRawInput(newTime, newTimeZone, newDate); // TODO: verify dt const [eventDateTime, eventDateTimeStr, eventInFuture, dateTimeValid] = getDateFromRawInput(newTime, newTimeZone, newDate);
if (!eventInFuture) { if (!eventInFuture || !dateTimeValid) {
bot.helpers.sendInteractionResponse(interaction.id, interaction.token, { bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
type: InteractionResponseTypes.ChannelMessageWithSource, type: InteractionResponseTypes.ChannelMessageWithSource,
data: { data: {
flags: ApplicationCommandFlags.Ephemeral, flags: ApplicationCommandFlags.Ephemeral,
embeds: [{ embeds: [{
color: warnColor, color: warnColor,
title: 'You cannot create an event in the past.', 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 date in the future', description: `Please dismiss this message and try again with a ${dateTimeValid ? 'date in the future' : 'valid date/time'}.`,
fields: [{ fields: dateTimeValid ? [{
name: 'Date/Time Entered:', name: 'Date/Time Entered:',
value: generateTimeFieldStr(eventDateTimeStr, eventDateTime), value: generateTimeFieldStr(eventDateTimeStr, eventDateTime),
}], }] : undefined,
}], }],
}, },
}).catch((e: Error) => utils.commonLoggers.interactionSendError('applyDateTime.ts', interaction, e)); }).catch((e: Error) => utils.commonLoggers.interactionSendError('applyDateTime.ts', interaction, e));