我在檢查檔案的命令目錄時遇到了一些問題。我想readdirSync從目錄中的某些目錄(音樂、資訊等)中讀取所有 .js 檔案,/commands/但我似乎不知道或無法找到一種方法來做到這一點。
下面的代碼:
import { readdirSync } from "fs";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export async function importer(client) {
const commandFiles = readdirSync(join(__dirname, "../../", "/commands/")).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = await import(join(__dirname, "../../", "/commands/", `${file}`));
client.commands.set(command.default.name, command.default);
}
}
在 commandFiles 中,我似乎無法在/in之后找到要放置的內容,commands因此它可以讀取/commands/.
任何幫助表示贊賞!
謝謝!
uj5u.com熱心網友回復:
讀取命令目錄后需要讀取子目錄,并使用子目錄名稱作為 join 的引數:
export async function importer(client) {
const dir = join(__dirname, "../../commands/")
const commandCategories = readdirSync(dir)
for (let cat of commandCategories) {
const commands = readdirSync(join(dir, cat)).filter(files => files.endsWith(".js"));
for (const file of commands) {
const command = import(join(dir, cat, file));
// Add any logic you want before you load the command here
client.commands.set(command.default.name, command.default);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465845.html
