Skip to content

Commit

Permalink
Merge pull request #11 from fac31/timestamp-command
Browse files Browse the repository at this point in the history
Timestamp command
  • Loading branch information
sulphite authored Apr 22, 2024
2 parents 3f75ee4 + 9f5dfee commit 303bd13
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ dist/**/*
# ignore yarn.lock
yarn.lock

pnpm-lock.yaml
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
},
"homepage": "https://github.com/fac31/Jana-Reza-Bot#jana-reza-bot",
"dependencies": {
"chrono-node": "^2.7.5",
"dayjs": "^1.11.10",
"discord.js": "^14.14.1",
"dotenv": "^16.4.5"
},
Expand Down
3 changes: 2 additions & 1 deletion src/commandCenter/Commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from "../Command";
import { Howdy } from "./commands/Howdy";
import { Time } from "./commands/Time";

export const Commands: Command[] = [
Howdy
Howdy, Time
];
47 changes: 47 additions & 0 deletions src/commandCenter/commands/Time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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) => {
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({
content,
ephemeral: true,
})
},
}

0 comments on commit 303bd13

Please sign in to comment.