【问题标题】:command handler only recognize the top-most folder instead of all the folders命令处理程序只识别最顶层的文件夹而不是所有文件夹
【发布时间】:2022-06-25 18:56:59
【问题描述】:

我有一个问题,我的命令处理程序只能识别我的命令目录中的顶级文件夹。它应该显示命令目录中的所有可用文件夹,但它只显示了顶部的“测试”类别。任何帮助将不胜感激。

文件夹/目录构建:

console.log 输出:

命令处理程序代码:

const {readdirSync} = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Commands");
table.setHeading('Category', 'Command', ' Load status');
var logged = false;
const path = require('node:path')

module.exports = (client) => {
readdirSync('./Commands/').forEach(dir => {
    var commands = readdirSync(`./Commands/${dir}/`).filter(file => file.endsWith('.js'));
    for(let file of commands){
        let pull = require(`../Commands/${dir}/${file}`);
        if(pull.name){
            client.commands.set(pull.name, pull);
            table.addRow(dir,file,'✔️   -> Command Loaded')
        } else {
            table.addRow(dir,file,'❌   -> Command Error')
            continue;
        }
        if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))       
    }
    if(!logged) {
        console.log(table.toString())
        console.log(`[Command] Command Handler is Ready! | Total Commands: ${commands.length}`)
        logged = true
    }
});
}

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    我相信您在遍历每个文件夹后会覆盖 commands 变量。试试这个:

    const {readdirSync} = require('fs');
    const ascii = require('ascii-table');
    let table = new ascii("Commands");
    table.setHeading('Category', 'Command', ' Load status');
    var logged = false;
    const path = require('node:path')
    
    module.exports = (client) => {
    readdirSync('./Commands/').forEach(dir => {
    
        var commands = []
        commands.push(readdirSync(`./Commands/${dir}/`).filter(file => file.endsWith('.js')));
    
        for(let file of commands){
            let pull = require(`../Commands/${dir}/${file}`);
            if(pull.name){
                client.commands.set(pull.name, pull);
                table.addRow(dir,file,'✔️   -> Command Loaded')
            } else {
                table.addRow(dir,file,'❌   -> Command Error')
                continue;
            }
            if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))       
        }
        if(!logged) {
            console.log(table.toString())
            console.log(`[Command] Command Handler is Ready! | Total Commands: ${commands.length}`)
            logged = true
        }
    });
    }
    

    如果这没有帮助,那么它可能仍然是我上面提到的那个问题,但我所做的编辑可能与您的代码不兼容。

    【讨论】:

    • 感谢您的回答,但我已经想通了,我只是将 'if(!logged)' 放在 readdirSync 范围之外。
    猜你喜欢
    • 2022-01-14
    • 2021-12-13
    • 2019-09-23
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多