From 6a31473d22c27d13c732d36f9b4a56bcb872dfe5 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sat, 30 Sep 2023 12:31:25 +0200 Subject: [PATCH 1/4] scripts: move scripts to the project root directory I believe, that these kind of scripts should reside there instead of src directory --- Dockerfile | 1 + package.json | 2 +- {src/scripts => scripts}/pushCommands.ts | 6 +++--- tsconfig.json | 3 ++- 4 files changed, 7 insertions(+), 5 deletions(-) rename {src/scripts => scripts}/pushCommands.ts (90%) diff --git a/Dockerfile b/Dockerfile index 4b9f015..9ef0693 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,7 @@ RUN npx prisma generate # Typescript compiling COPY tsconfig.json . COPY src ./src +COPY scripts ./scripts RUN npx tsc # Run the app diff --git a/package.json b/package.json index feb71a3..a3dee49 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "tsc && node dist/src/index.js", "test": "echo \"Error: no test specified\" && exit 1", - "publishCommands": "tsc && node dist/src/scripts/pushCommands.js" + "publishCommands": "tsc && node dist/scripts/pushCommands.js" }, "author": "Wroclaw", "license": "MIT", diff --git a/src/scripts/pushCommands.ts b/scripts/pushCommands.ts similarity index 90% rename from src/scripts/pushCommands.ts rename to scripts/pushCommands.ts index 3f88ee5..b6419fc 100644 --- a/src/scripts/pushCommands.ts +++ b/scripts/pushCommands.ts @@ -1,15 +1,15 @@ // https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands import { REST, RESTGetAPIOAuth2CurrentApplicationResult, RESTPostAPIApplicationCommandsJSONBody, Routes } from "discord.js"; -import { config } from "../index"; +import { config } from "../src/index"; import requireDirectory from "require-directory"; -import Command from "../command"; +import Command from "../src/command"; const post: RESTPostAPIApplicationCommandsJSONBody[] = []; const guildId = process.argv.slice(2)[0]; -requireDirectory<{default: Command}, void>(module, "../commands", { +requireDirectory<{default: Command}, void>(module, "../src/commands", { visit: function (obj) { console.log(obj); // eslint-disable-next-line @typescript-eslint/ban-ts-comment diff --git a/tsconfig.json b/tsconfig.json index 77c5666..173e3c9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "include": [ - "./src/**/*" + "./src/**/*", + "./scripts/**/*" ], "compilerOptions": { "target": "ES2022", From 4729f7f56339116600469d4c681cfbb4642f610a Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sat, 30 Sep 2023 13:23:33 +0200 Subject: [PATCH 2/4] /check-limit: rename to check-quota and change description to match it to the new reality, limits are named quotas now description of an option previously was longer than 100 characters which discord api didn't like it --- src/commands/{check-limit.ts => check-quota.ts} | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) rename src/commands/{check-limit.ts => check-quota.ts} (91%) diff --git a/src/commands/check-limit.ts b/src/commands/check-quota.ts similarity index 91% rename from src/commands/check-limit.ts rename to src/commands/check-quota.ts index 14c0f58..3a2f5ef 100644 --- a/src/commands/check-limit.ts +++ b/src/commands/check-quota.ts @@ -4,13 +4,13 @@ import Command from "../command"; import { config } from "../index"; export default class MyLimit extends Command implements Command { - name = "check-limit"; - description = "Checks your limit and usage"; + name = "check-quota"; + description = "Checks your quota and usage"; type = ApplicationCommandType.ChatInput; options: APIApplicationCommandOption[] = [ { name: "recovery-for", - description: "Calculate the limit recovery time for given message count (default: amount required to use the bot again or 1)", + description: "Get the recovery time for given quota units count (default: until can use the bot or 1)", type: ApplicationCommandOptionType.Integer, required: false, }, @@ -18,6 +18,7 @@ export default class MyLimit extends Command implements Command { name: "ephemeral", description: "if true, only you can see the response (default true)", type: ApplicationCommandOptionType.Boolean, + required: false, } ]; From a05047ab7df11ec332505ac7ebe014349111d244 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sat, 30 Sep 2023 14:47:10 +0200 Subject: [PATCH 3/4] script/pushCommands: make sure the script exits --- scripts/pushCommands.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/pushCommands.ts b/scripts/pushCommands.ts index b6419fc..39ad319 100644 --- a/scripts/pushCommands.ts +++ b/scripts/pushCommands.ts @@ -37,6 +37,8 @@ const rest = new REST().setToken(config.tokens.Discord); ); } console.log("Refreshed successfully"); + process.exit(0); })().catch( e => { console.error(e); + process.exit(1); }); From a186ba9e8011a24cb3a90ee0a048c2bc193812a8 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Thu, 12 Oct 2023 11:55:53 +0200 Subject: [PATCH 4/4] scripts/pushCommands: Refactor how commands are being read this should reduce errors when reading commands also this won't fail if reading one command fails --- scripts/pushCommands.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/scripts/pushCommands.ts b/scripts/pushCommands.ts index 39ad319..25ee04e 100644 --- a/scripts/pushCommands.ts +++ b/scripts/pushCommands.ts @@ -9,15 +9,21 @@ import Command from "../src/command"; const post: RESTPostAPIApplicationCommandsJSONBody[] = []; const guildId = process.argv.slice(2)[0]; -requireDirectory<{default: Command}, void>(module, "../src/commands", { - visit: function (obj) { - console.log(obj); +const importedCommands = requireDirectory(module, "../src/commands"); + +for (const obj in importedCommands) { + try { + const allExports = importedCommands[obj] as {default: unknown}; + const defaultExport = allExports.default; // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call - post.push(new obj.default().toRESTPostApplicationCommands()); - }, -}); + // @ts-expect-error + const constructedExport = new defaultExport() as unknown; + if (!(constructedExport instanceof Command)) throw new Error(`${obj}'s default does not extends Command`); + post.push(constructedExport.toRESTPostApplicationCommands()); + } catch (e) { + console.error(e); + } +} const rest = new REST().setToken(config.tokens.Discord);