Skip to content

Commit

Permalink
responds with copiable timestamp code
Browse files Browse the repository at this point in the history
  • Loading branch information
sulphite committed Apr 18, 2024
1 parent 1c5bdbc commit 9f5dfee
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions src/commandCenter/commands/Time.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
import { CommandInteraction, Client, ApplicationCommandOptionType, ApplicationCommandType } from "discord.js";
import { Command } from "../../Command";
import dayjs from "dayjs";
import * as chrono from "chrono-node"
import {
CommandInteraction,
Client,
ApplicationCommandOptionType,
ApplicationCommandType,
} from 'discord.js'
import { Command } from '../../Command'
import dayjs from 'dayjs'
import * as chrono from 'chrono-node'

export const Time: Command = {
name: "time",
description: "turn a local time into a global timestamp",
type: ApplicationCommandType.ChatInput,
options: [{
name: "when",
description: "a time like 'tomorrow at 4pm' or 'next week sunday at 12'",
type: ApplicationCommandOptionType.String,
required: true,
}],
run: async (_: Client, interaction: CommandInteraction) => {
interaction.createdAt
// const sentTime = dayjs(interaction.createdAt)
const userInput = interaction.options.get("when")?.value
let targetTime;
console.log(userInput)
if (userInput) {
targetTime = chrono.parseDate(userInput.toString(), {
instant: interaction.createdAt
}, { forwardDate: true })
console.log(targetTime)
} else {
targetTime = null
throw new Error("couldnt get user input")
}
// console.log("sent:", sentTime)
const content = targetTime ? `Did you mean <t:${dayjs(targetTime).unix()}>` : "Sorry, I didn't understand."
name: 'time',
description: 'turn a local time into a global timestamp',
type: ApplicationCommandType.ChatInput,
options: [
{
name: 'when',
description:
"a time like 'tomorrow at 4pm' or 'next week sunday at 12'",
type: ApplicationCommandOptionType.String,
required: true,
},
],
run: async (_: Client, interaction: CommandInteraction) => {
const userInput = interaction.options.get('when')?.value
let targetTime
console.log(userInput)
if (userInput) {
targetTime = chrono.parseDate(
userInput.toString(),
{ instant: interaction.createdAt },
{ forwardDate: true }
)
} else {
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()}>\``
: "Sorry, I didn't understand."

await interaction.followUp({
//https://discordjs.guide/slash-commands/response-methods.html#ephemeral-responses
content
});
}
await interaction.followUp({
content,
ephemeral: true,
})
},
}

0 comments on commit 9f5dfee

Please sign in to comment.