diff --git a/src/commandCenter/commands/Time.ts b/src/commandCenter/commands/Time.ts index 7b327d4..bd54980 100644 --- a/src/commandCenter/commands/Time.ts +++ b/src/commandCenter/commands/Time.ts @@ -6,7 +6,7 @@ import { } from 'discord.js' import { Command } from '../../Command' import dayjs from 'dayjs' -import * as chrono from 'chrono-node' +import { stringToTime } from '../utils/stringToTime' export const Time: Command = { name: 'time', @@ -22,18 +22,13 @@ export const Time: Command = { }, ], run: async (_: Client, interaction: CommandInteraction) => { - const userInput = interaction.options.get('when')?.value + const userInput = interaction.options.get('when')?.value as string let targetTime - console.log(userInput) - if (userInput) { - targetTime = chrono.parseDate( - userInput.toString(), - { instant: interaction.createdAt }, - { forwardDate: true } - ) - } else { + try { + targetTime = stringToTime(userInput, interaction.createdAt) + } catch (error) { + console.error(error) targetTime = null - throw new Error('couldnt get user input') } const content = targetTime ? `Here's your timestamp for \`\`` diff --git a/src/commandCenter/utils/stringToTime.ts b/src/commandCenter/utils/stringToTime.ts new file mode 100644 index 0000000..3f745a5 --- /dev/null +++ b/src/commandCenter/utils/stringToTime.ts @@ -0,0 +1,13 @@ +import * as chrono from 'chrono-node' + +export const stringToTime = (input: string, created: Date): Date => { + const time = chrono.parseDate( + input, + { instant: created }, + { forwardDate: true } + ) + if (time === null) { + throw new Error('couldnt parse') + } + return time +}