From db020749feafa9e598157e72cb69df4bf0f83e26 Mon Sep 17 00:00:00 2001 From: Janett Gerrard Date: Tue, 16 Apr 2024 16:13:10 +0100 Subject: [PATCH 1/4] install day.js --- .gitignore | 1 + package-lock.json | 6 ++++++ package.json | 1 + 3 files changed, 8 insertions(+) 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..78a36b8 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "homepage": "https://github.com/fac31/Jana-Reza-Bot#jana-reza-bot", "dependencies": { + "dayjs": "^1.11.10", "discord.js": "^14.14.1", "dotenv": "^16.4.5" }, From f7360f537a0f3a26282b2f76ee6fa3b18ff92716 Mon Sep 17 00:00:00 2001 From: Janett Gerrard Date: Tue, 16 Apr 2024 17:47:52 +0100 Subject: [PATCH 2/4] create Time command --- src/commandCenter/Commands.ts | 3 ++- src/commandCenter/commands/Time.ts | 39 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/commandCenter/commands/Time.ts 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..5968137 --- /dev/null +++ b/src/commandCenter/commands/Time.ts @@ -0,0 +1,39 @@ +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 ` : "Sorry, I didn't understand." + + await interaction.followUp({ + //https://discordjs.guide/slash-commands/response-methods.html#ephemeral-responses + content + }); + } +} From 1c5bdbcec61628a1e9104bb76ad3c5a8a3da1888 Mon Sep 17 00:00:00 2001 From: Janett Gerrard Date: Tue, 16 Apr 2024 17:48:18 +0100 Subject: [PATCH 3/4] install chrono for date parsing --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 78a36b8..08ec74b 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "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" From 9f5dfee31a5d514bb1840a64b17fff82a09eae7e Mon Sep 17 00:00:00 2001 From: Janett Gerrard Date: Thu, 18 Apr 2024 10:01:02 +0100 Subject: [PATCH 4/4] responds with copiable timestamp code --- src/commandCenter/commands/Time.ts | 78 ++++++++++++++++-------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/src/commandCenter/commands/Time.ts b/src/commandCenter/commands/Time.ts index 5968137..7b327d4 100644 --- a/src/commandCenter/commands/Time.ts +++ b/src/commandCenter/commands/Time.ts @@ -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 ` : "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 \`\`` + : "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, + }) + }, }