Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor functions #14

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/commandCenter/commands/Time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 <t:${dayjs(targetTime).unix()}> \`<t:${dayjs(targetTime).unix()}>\``
Expand Down
13 changes: 13 additions & 0 deletions src/commandCenter/utils/stringToTime.ts
Original file line number Diff line number Diff line change
@@ -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
}