【问题标题】:Making commands into subfolders将命令放入子文件夹
【发布时间】:2020-06-20 00:39:24
【问题描述】:

我正在努力做到这一点,以便我可以将我的命令添加到子文件夹中以更好地组织它们,但这些命令仍然可以工作。我试图弄清楚如何将我的代码更改为这种方式,但我的代码看起来与其他人的完全不同,我有点卡住了。

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    你可以试试这个:

    const client = new Discord.Client();
    client.commands = new Discord.Collection();
    
    const folders = fs.readdirSync('./commands'); // read the directory of folders
    
    for (var folder of folders) {
        const files = fs.readdirSync(`./commands/${folder}`); // for each folder, read the files in the folder
        for (var file of files) {
            const command = require(`./commands/${folder}/${file}`); // for each file, set the command
            client.commands.set(command.name, command);
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 所以我尝试了这个并在启动机器人时收到一个错误:找不到模块'./commands/hug.js'。我将我的 hug.js 命令移动到“命令”下的子文件夹中
    • 哦,哎呀。我犯了一个错误。我已经修好了。
    • 很抱歉,我自己没有发现这一点,但更新代码运行良好。非常感谢您的帮助
    猜你喜欢
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2016-01-20
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多