diff --git a/.gitignore b/.gitignore index bd23632..6cf9853 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ dist/**/* # ignore yarn.lock yarn.lock +pnpm-lock.yaml diff --git a/package-lock.json b/package-lock.json index 94dfc3f..dbd0300 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "dayjs": "^1.11.10", "discord.js": "^14.14.1", "dotenv": "^16.4.5" }, @@ -1012,6 +1013,11 @@ "node": ">= 8" } }, + "node_modules/dayjs": { + "version": "1.11.10", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz", + "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==" + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index 8fdb522..08ec74b 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/commandCenter/Commands.ts b/src/commandCenter/Commands.ts index a5f7404..418d3b3 100644 --- a/src/commandCenter/Commands.ts +++ b/src/commandCenter/Commands.ts @@ -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 ]; diff --git a/src/commandCenter/commands/Time.ts b/src/commandCenter/commands/Time.ts new file mode 100644 index 0000000..7b327d4 --- /dev/null +++ b/src/commandCenter/commands/Time.ts @@ -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 \`\`` + : "Sorry, I didn't understand." + + await interaction.followUp({ + content, + ephemeral: true, + }) + }, +}