From cc89bdf2b4a70113d827fc82c9018bd3ebe4c42d Mon Sep 17 00:00:00 2001 From: Janett Gerrard Date: Tue, 23 Apr 2024 11:50:14 +0100 Subject: [PATCH 1/2] move chrono parser logic to separate func --- src/commandCenter/commands/Time.ts | 17 ++++++----------- src/commandCenter/utils/stringToTime.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 src/commandCenter/utils/stringToTime.ts 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..4c407c3 --- /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 => { + let time = chrono.parseDate( + input, + { instant: created }, + { forwardDate: true } + ) + if (time === null) { + throw new Error('couldnt parse') + } + return time +} From c95454a1c024fa30082a540c0567ec0202b1a7dd Mon Sep 17 00:00:00 2001 From: Janett Gerrard Date: Tue, 23 Apr 2024 12:29:26 +0100 Subject: [PATCH 2/2] lint fix --- src/commandCenter/utils/stringToTime.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commandCenter/utils/stringToTime.ts b/src/commandCenter/utils/stringToTime.ts index 4c407c3..3f745a5 100644 --- a/src/commandCenter/utils/stringToTime.ts +++ b/src/commandCenter/utils/stringToTime.ts @@ -1,7 +1,7 @@ import * as chrono from 'chrono-node' export const stringToTime = (input: string, created: Date): Date => { - let time = chrono.parseDate( + const time = chrono.parseDate( input, { instant: created }, { forwardDate: true }