From 79b79b6a44967d9ed1e449b49a851b417afb263b Mon Sep 17 00:00:00 2001 From: Muhammad Nizamuddin Aulia Date: Mon, 17 Mar 2025 01:38:48 +0700 Subject: [PATCH] fix(create-discord-bot): register command files in subdirectories (#10775) * feat(create-discord-bot): add user command in utility subdirectory * fix(create-discord-bot): command files in the subdirectory were skipped * fix(create-discord-bot): fix command files in subdirectory were skipped in Deno template * fix: minor fix * fix: lint * refactor: suggested changes revert: unrelated change --------- Co-authored-by: almeidx Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- .../Deno/src/commands/utility/user.ts | 11 ++++++++ .../template/Deno/src/util/loaders.ts | 26 ++++++++++--------- .../JavaScript/src/commands/utility/user.js | 10 +++++++ .../template/JavaScript/src/util/loaders.js | 24 +++++++++-------- .../TypeScript/src/commands/utility/user.ts | 11 ++++++++ .../template/TypeScript/src/util/loaders.ts | 24 +++++++++-------- 6 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 packages/create-discord-bot/template/Deno/src/commands/utility/user.ts create mode 100644 packages/create-discord-bot/template/JavaScript/src/commands/utility/user.js create mode 100644 packages/create-discord-bot/template/TypeScript/src/commands/utility/user.ts diff --git a/packages/create-discord-bot/template/Deno/src/commands/utility/user.ts b/packages/create-discord-bot/template/Deno/src/commands/utility/user.ts new file mode 100644 index 000000000..34bc5315a --- /dev/null +++ b/packages/create-discord-bot/template/Deno/src/commands/utility/user.ts @@ -0,0 +1,11 @@ +import type { Command } from '../index.ts'; + +export default { + data: { + name: 'user', + description: 'Provides information about the user.', + }, + async execute(interaction) { + await interaction.reply(`This command was run by ${interaction.user.username}.`); + }, +} satisfies Command; diff --git a/packages/create-discord-bot/template/Deno/src/util/loaders.ts b/packages/create-discord-bot/template/Deno/src/util/loaders.ts index 46d991323..1c2ab9ea6 100644 --- a/packages/create-discord-bot/template/Deno/src/util/loaders.ts +++ b/packages/create-discord-bot/template/Deno/src/util/loaders.ts @@ -1,6 +1,6 @@ import type { PathLike } from 'node:fs'; import { readdir, stat } from 'node:fs/promises'; -import { URL } from 'node:url'; +import { fileURLToPath, URL } from 'node:url'; import type { Command } from '../commands/index.ts'; import { predicate as commandPredicate } from '../commands/index.ts'; import type { Event } from '../events/index.ts'; @@ -40,22 +40,24 @@ export async function loadStructures( // Loop through all the files in the directory for (const file of files) { - // If the file is index.js or the file does not end with .js, skip the file + const fileUrl = new URL(`${dir}/${file}`, import.meta.url); + + // Get the stats of the file + const statFile = await stat(fileUrl); + + // If the file is a directory and recursive is true, recursively load the structures in the directory + if (statFile.isDirectory() && recursive) { + structures.push(...(await loadStructures(fileUrl, predicate, recursive))); + continue; + } + + // If the file is index.ts or the file does not end with .ts, skip the file if (file === 'index.ts' || !file.endsWith('.ts')) { continue; } - // Get the stats of the file - const statFile = await stat(new URL(`${dir}/${file}`)); - - // If the file is a directory and recursive is true, recursively load the structures in the directory - if (statFile.isDirectory() && recursive) { - structures.push(...(await loadStructures(`${dir}/${file}`, predicate, recursive))); - continue; - } - // Import the structure dynamically from the file - const structure = (await import(`${dir}/${file}`)).default; + const structure = (await import(fileURLToPath(fileUrl))).default; // If the structure is a valid structure, add it if (predicate(structure)) { diff --git a/packages/create-discord-bot/template/JavaScript/src/commands/utility/user.js b/packages/create-discord-bot/template/JavaScript/src/commands/utility/user.js new file mode 100644 index 000000000..9c6fcbce0 --- /dev/null +++ b/packages/create-discord-bot/template/JavaScript/src/commands/utility/user.js @@ -0,0 +1,10 @@ +/** @type {import('../index.js').Command} */ +export default { + data: { + name: 'user', + description: 'Provides information about the user.', + }, + async execute(interaction) { + await interaction.reply(`This command was run by ${interaction.user.username}.`); + }, +}; diff --git a/packages/create-discord-bot/template/JavaScript/src/util/loaders.js b/packages/create-discord-bot/template/JavaScript/src/util/loaders.js index c3c437573..81776ad72 100644 --- a/packages/create-discord-bot/template/JavaScript/src/util/loaders.js +++ b/packages/create-discord-bot/template/JavaScript/src/util/loaders.js @@ -1,5 +1,5 @@ import { readdir, stat } from 'node:fs/promises'; -import { URL } from 'node:url'; +import { fileURLToPath, URL } from 'node:url'; import { predicate as commandPredicate } from '../commands/index.js'; import { predicate as eventPredicate } from '../events/index.js'; @@ -37,22 +37,24 @@ export async function loadStructures(dir, predicate, recursive = true) { // Loop through all the files in the directory for (const file of files) { + const fileUrl = new URL(`${dir}/${file}`, import.meta.url); + + // Get the stats of the file + const statFile = await stat(fileUrl); + + // If the file is a directory and recursive is true, recursively load the structures in the directory + if (statFile.isDirectory() && recursive) { + structures.push(...(await loadStructures(fileUrl, predicate, recursive))); + continue; + } + // If the file is index.js or the file does not end with .js, skip the file if (file === 'index.js' || !file.endsWith('.js')) { continue; } - // Get the stats of the file - const statFile = await stat(new URL(`${dir}/${file}`)); - - // If the file is a directory and recursive is true, recursively load the structures in the directory - if (statFile.isDirectory() && recursive) { - structures.push(...(await loadStructures(`${dir}/${file}`, predicate, recursive))); - continue; - } - // Import the structure dynamically from the file - const structure = (await import(`${dir}/${file}`)).default; + const structure = (await import(fileURLToPath(fileUrl))).default; // If the structure is a valid structure, add it if (predicate(structure)) { diff --git a/packages/create-discord-bot/template/TypeScript/src/commands/utility/user.ts b/packages/create-discord-bot/template/TypeScript/src/commands/utility/user.ts new file mode 100644 index 000000000..121d4cec7 --- /dev/null +++ b/packages/create-discord-bot/template/TypeScript/src/commands/utility/user.ts @@ -0,0 +1,11 @@ +import type { Command } from '../index.[REPLACE_IMPORT_EXT]'; + +export default { + data: { + name: 'user', + description: 'Provides information about the user.', + }, + async execute(interaction) { + await interaction.reply(`This command was run by ${interaction.user.username}.`); + }, +} satisfies Command; diff --git a/packages/create-discord-bot/template/TypeScript/src/util/loaders.ts b/packages/create-discord-bot/template/TypeScript/src/util/loaders.ts index 7f1f89c51..cb79ff7a6 100644 --- a/packages/create-discord-bot/template/TypeScript/src/util/loaders.ts +++ b/packages/create-discord-bot/template/TypeScript/src/util/loaders.ts @@ -1,6 +1,6 @@ import type { PathLike } from 'node:fs'; import { readdir, stat } from 'node:fs/promises'; -import { URL } from 'node:url'; +import { fileURLToPath, URL } from 'node:url'; import type { Command } from '../commands/index.[REPLACE_IMPORT_EXT]'; import { predicate as commandPredicate } from '../commands/index.[REPLACE_IMPORT_EXT]'; import type { Event } from '../events/index.[REPLACE_IMPORT_EXT]'; @@ -40,22 +40,24 @@ export async function loadStructures( // Loop through all the files in the directory for (const file of files) { + const fileUrl = new URL(`${dir}/${file}`, import.meta.url); + + // Get the stats of the file + const statFile = await stat(fileUrl); + + // If the file is a directory and recursive is true, recursively load the structures in the directory + if (statFile.isDirectory() && recursive) { + structures.push(...(await loadStructures(fileUrl, predicate, recursive))); + continue; + } + // If the file is index.[REPLACE_IMPORT_EXT] or the file does not end with .[REPLACE_IMPORT_EXT], skip the file if (file === 'index.[REPLACE_IMPORT_EXT]' || !file.endsWith('.[REPLACE_IMPORT_EXT]')) { continue; } - // Get the stats of the file - const statFile = await stat(new URL(`${dir}/${file}`)); - - // If the file is a directory and recursive is true, recursively load the structures in the directory - if (statFile.isDirectory() && recursive) { - structures.push(...(await loadStructures(`${dir}/${file}`, predicate, recursive))); - continue; - } - // Import the structure dynamically from the file - const structure = (await import(`${dir}/${file}`)).default; + const structure = (await import(fileURLToPath(fileUrl))).default; // If the structure is a valid structure, add it if (predicate(structure)) structures.push(structure);