mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
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 <github@almeidx.dev> Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
eabcc52594
commit
79b79b6a44
@@ -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;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { PathLike } from 'node:fs';
|
import type { PathLike } from 'node:fs';
|
||||||
import { readdir, stat } from 'node:fs/promises';
|
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 type { Command } from '../commands/index.ts';
|
||||||
import { predicate as commandPredicate } from '../commands/index.ts';
|
import { predicate as commandPredicate } from '../commands/index.ts';
|
||||||
import type { Event } from '../events/index.ts';
|
import type { Event } from '../events/index.ts';
|
||||||
@@ -40,22 +40,24 @@ export async function loadStructures<T>(
|
|||||||
|
|
||||||
// Loop through all the files in the directory
|
// Loop through all the files in the directory
|
||||||
for (const file of files) {
|
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')) {
|
if (file === 'index.ts' || !file.endsWith('.ts')) {
|
||||||
continue;
|
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
|
// 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 the structure is a valid structure, add it
|
||||||
if (predicate(structure)) {
|
if (predicate(structure)) {
|
||||||
|
|||||||
@@ -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}.`);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { readdir, stat } from 'node:fs/promises';
|
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 commandPredicate } from '../commands/index.js';
|
||||||
import { predicate as eventPredicate } from '../events/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
|
// Loop through all the files in the directory
|
||||||
for (const file of files) {
|
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 the file is index.js or the file does not end with .js, skip the file
|
||||||
if (file === 'index.js' || !file.endsWith('.js')) {
|
if (file === 'index.js' || !file.endsWith('.js')) {
|
||||||
continue;
|
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
|
// 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 the structure is a valid structure, add it
|
||||||
if (predicate(structure)) {
|
if (predicate(structure)) {
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { PathLike } from 'node:fs';
|
import type { PathLike } from 'node:fs';
|
||||||
import { readdir, stat } from 'node:fs/promises';
|
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 type { Command } from '../commands/index.[REPLACE_IMPORT_EXT]';
|
||||||
import { predicate as commandPredicate } 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]';
|
import type { Event } from '../events/index.[REPLACE_IMPORT_EXT]';
|
||||||
@@ -40,22 +40,24 @@ export async function loadStructures<T>(
|
|||||||
|
|
||||||
// Loop through all the files in the directory
|
// Loop through all the files in the directory
|
||||||
for (const file of files) {
|
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 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]')) {
|
if (file === 'index.[REPLACE_IMPORT_EXT]' || !file.endsWith('.[REPLACE_IMPORT_EXT]')) {
|
||||||
continue;
|
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
|
// 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 the structure is a valid structure, add it
|
||||||
if (predicate(structure)) structures.push(structure);
|
if (predicate(structure)) structures.push(structure);
|
||||||
|
|||||||
Reference in New Issue
Block a user