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
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()),
];
};

View File

@ -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();
}

View File

@ -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 {

View File

@ -56,6 +56,7 @@ export const generateActionRow = (baseValue: string, activities: Array<Activity>
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<LFGMember>,
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:',

View File

@ -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));