diff --git a/packages/commandkit/bin/build.mjs b/packages/commandkit/bin/build.mjs index 3997599..bafcc24 100644 --- a/packages/commandkit/bin/build.mjs +++ b/packages/commandkit/bin/build.mjs @@ -1,10 +1,10 @@ // @ts-check +import { appendFile } from 'node:fs/promises'; +import { join } from 'node:path'; import { build } from 'tsup'; import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs'; import ora from 'ora'; -import { appendFile } from 'node:fs/promises'; -import { join } from 'node:path'; export async function bootstrapProductionBuild(config) { const { diff --git a/packages/commandkit/bin/common.mjs b/packages/commandkit/bin/common.mjs index 3f1ca37..d717fd1 100644 --- a/packages/commandkit/bin/common.mjs +++ b/packages/commandkit/bin/common.mjs @@ -1,8 +1,8 @@ // @ts-check +import { rimrafSync } from 'rimraf'; import { join } from 'node:path'; import fs from 'node:fs'; -import { rimrafSync } from 'rimraf'; const resetColor = '\x1b[0m'; diff --git a/packages/commandkit/bin/development.mjs b/packages/commandkit/bin/development.mjs index 3f5dd30..df35e55 100644 --- a/packages/commandkit/bin/development.mjs +++ b/packages/commandkit/bin/development.mjs @@ -1,11 +1,11 @@ // @ts-check import { config as dotenv } from 'dotenv'; -import { build } from 'tsup'; -import child_process from 'node:child_process'; -import ora from 'ora'; import { join } from 'node:path'; +import { build } from 'tsup'; import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs'; import { parseEnv } from './parse-env.mjs'; +import child_process from 'node:child_process'; +import ora from 'ora'; const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/; const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/; diff --git a/packages/commandkit/bin/index.mjs b/packages/commandkit/bin/index.mjs index b92556a..79e7d11 100644 --- a/packages/commandkit/bin/index.mjs +++ b/packages/commandkit/bin/index.mjs @@ -4,8 +4,8 @@ import { Command } from 'commander'; import { bootstrapDevelopmentServer } from './development.mjs'; -import { bootstrapProductionBuild } from './build.mjs'; import { bootstrapProductionServer } from './production.mjs'; +import { bootstrapProductionBuild } from './build.mjs'; const program = new Command('commandkit'); diff --git a/packages/commandkit/bin/production.mjs b/packages/commandkit/bin/production.mjs index 6bb0c64..2dc2163 100644 --- a/packages/commandkit/bin/production.mjs +++ b/packages/commandkit/bin/production.mjs @@ -1,10 +1,10 @@ // @ts-check import { config as dotenv } from 'dotenv'; -import child_process from 'node:child_process'; +import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { Colors, findCommandKitConfig, panic, write } from './common.mjs'; -import { existsSync } from 'node:fs'; import { parseEnv } from './parse-env.mjs'; +import child_process from 'node:child_process'; export async function bootstrapProductionServer(config) { const { diff --git a/packages/commandkit/src/handlers/command-handler/CommandHandler.ts b/packages/commandkit/src/handlers/command-handler/CommandHandler.ts index 7ce91e2..814f67f 100644 --- a/packages/commandkit/src/handlers/command-handler/CommandHandler.ts +++ b/packages/commandkit/src/handlers/command-handler/CommandHandler.ts @@ -3,12 +3,12 @@ import type { CommandFileObject, ReloadOptions } from '../../typings'; import { toFileURL } from '../../utils/resolve-file-url'; import { getFilePaths } from '../../utils/get-paths'; +import { clone } from '../../utils/clone'; import loadCommandsWithRest from './functions/loadCommandsWithRest'; import registerCommands from './functions/registerCommands'; -import builtInValidations from './validations'; +import builtInValidationsFunctions from './validations'; import colors from '../../utils/colors'; -import { clone } from '../../utils/clone'; /** * A handler for client application commands. @@ -27,7 +27,7 @@ export class CommandHandler { async init() { await this.#buildCommands(); - this.#buildValidations(); + this.#buildBuiltInValidations(); const devOnlyCommands = this.#data.commands.filter((cmd) => cmd.options?.devOnly); @@ -150,9 +150,9 @@ export class CommandHandler { } } - #buildValidations() { - for (const validationFunction of builtInValidations) { - this.#data.builtInValidations.push(validationFunction); + #buildBuiltInValidations() { + for (const builtInValidationFunction of builtInValidationsFunctions) { + this.#data.builtInValidations.push(builtInValidationFunction); } } @@ -175,7 +175,7 @@ export class CommandHandler { const { data, options, run, autocompleteRun, ...rest } = targetCommand; - // skip if autocomplete handler is not defined + // Skip if autocomplete handler is not defined if (isAutocomplete && !autocompleteRun) return; const commandObj = { @@ -239,9 +239,15 @@ export class CommandHandler { } async reloadCommands(type?: ReloadOptions) { + if (!this.#data.commandsPath) { + throw new Error( + 'Cannot reload commands as "commandsPath" was not provided when instantiating CommandKit.', + ); + } + this.#data.commands = []; - // Rebuild commands tree + // Re-build commands tree await this.#buildCommands(); if (this.#data.bulkRegister) { @@ -262,6 +268,4 @@ export class CommandHandler { }); } } - - async useUpdatedValidations() {} } diff --git a/packages/commandkit/src/handlers/command-handler/validations/devOnly.ts b/packages/commandkit/src/handlers/command-handler/validations/devOnly.ts index cb043af..682baa7 100644 --- a/packages/commandkit/src/handlers/command-handler/validations/devOnly.ts +++ b/packages/commandkit/src/handlers/command-handler/validations/devOnly.ts @@ -2,6 +2,7 @@ import type { BuiltInValidationParams } from '../typings'; export default function ({ interaction, targetCommand, handlerData }: BuiltInValidationParams) { if (interaction.isAutocomplete()) return; + if (targetCommand.options?.devOnly) { if (interaction.inGuild() && !handlerData.devGuildIds.includes(interaction.guildId)) { interaction.reply({ diff --git a/packages/commandkit/src/handlers/event-handler/EventHandler.ts b/packages/commandkit/src/handlers/event-handler/EventHandler.ts index c0f8e92..8b6b929 100644 --- a/packages/commandkit/src/handlers/event-handler/EventHandler.ts +++ b/packages/commandkit/src/handlers/event-handler/EventHandler.ts @@ -2,8 +2,8 @@ import type { EventHandlerOptions, EventHandlerData } from './typings'; import type { CommandHandler } from '../command-handler/CommandHandler'; import { getFilePaths, getFolderPaths } from '../../utils/get-paths'; import { toFileURL } from '../../utils/resolve-file-url'; -import colors from '../../utils/colors'; import { clone } from '../../utils/clone'; +import colors from '../../utils/colors'; /** * A handler for client events. @@ -97,6 +97,12 @@ export class EventHandler { } async reloadEvents(commandHandler?: CommandHandler) { + if (!this.#data.eventsPath) { + throw new Error( + 'Cannot reload events as "eventsPath" was not provided when instantiating CommandKit.', + ); + } + this.#data.events = []; await this.#buildEvents(); diff --git a/packages/commandkit/src/handlers/validation-handler/ValidationHandler.ts b/packages/commandkit/src/handlers/validation-handler/ValidationHandler.ts index 350aae6..5b52646 100644 --- a/packages/commandkit/src/handlers/validation-handler/ValidationHandler.ts +++ b/packages/commandkit/src/handlers/validation-handler/ValidationHandler.ts @@ -1,8 +1,8 @@ import type { ValidationHandlerData, ValidationHandlerOptions } from './typings'; import { toFileURL } from '../../utils/resolve-file-url'; import { getFilePaths } from '../../utils/get-paths'; -import colors from '../../utils/colors'; import { clone } from '../../utils/clone'; +import colors from '../../utils/colors'; /** * A handler for command validations. @@ -18,7 +18,7 @@ export class ValidationHandler { } async init() { - await this.#buildValidations(); + this.#data.validations = await this.#buildValidations(); } async #buildValidations() { @@ -27,6 +27,8 @@ export class ValidationHandler { const validationPaths = await getFilePaths(this.#data.validationsPath, true); const validationFilePaths = validationPaths.filter((path) => allowedExtensions.test(path)); + const validationFunctions: Function[] = []; + for (const validationFilePath of validationFilePaths) { const modulePath = toFileURL(validationFilePath); @@ -54,8 +56,10 @@ export class ValidationHandler { continue; } - this.#data.validations.push(validationFunction); + validationFunctions.push(validationFunction); } + + return validationFunctions; } get validations() { @@ -63,8 +67,14 @@ export class ValidationHandler { } async reloadValidations() { - this.#data.validations = []; + if (!this.#data.validationsPath) { + throw new Error( + 'Cannot reload validations as "validationsPath" was not provided when instantiating CommandKit.', + ); + } + + const newValidations = await this.#buildValidations(); - await this.#buildValidations(); + this.#data.validations = newValidations; } } diff --git a/packages/commandkit/tests/src/commands/misc/ping.ts b/packages/commandkit/tests/src/commands/misc/ping.ts index 40d11c0..26568cc 100644 --- a/packages/commandkit/tests/src/commands/misc/ping.ts +++ b/packages/commandkit/tests/src/commands/misc/ping.ts @@ -46,7 +46,7 @@ export async function run({ interaction, client }: SlashCommandProps) { const row = new ActionRowBuilder().addComponents(button); const message = await interaction.reply({ - content: 'Click the button', + content: 'Click one of the buttons', components: [row], fetchReply: true, });