fs.watch自动重新加载命令

时间:2018-11-07 22:16:03

标签: node.js fs discord.js

我一直在研究Discord Bot Rxiqi,并且一直想要在文件更改时自动重载命令...我的代码是

var folder = "./commands/";
fs.watch(folder, { encoding: 'UTF-8' }, (eventType, filename) => {
    if (eventType ==="change") {
        console.log(`Updating Command: ${folder+filename}`)
        delete require.cache[require.resolve(folder+filename)];
        client.commands.delete(folder+filename);
        const props = require(filename);
        client.commands.set(folder+filename,props);
    }
});

但是我一直得到这个控制台输出:

Updating Command: ./commands/adverts.js
Error: Cannot find module './commands/adverts.js'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.resolve (internal/module.js:18:19)
    at FSWatcher.fs.watch (E:\BotDevelopment\Rxiqi\status\ready.js:62:38)
    at emitTwo (events.js:126:13)
    at FSWatcher.emit (events.js:214:7)
    at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)
Updating Command: ./commands/adverts.js
Error: Cannot find module './commands/adverts.js'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.resolve (internal/module.js:18:19)
    at FSWatcher.fs.watch (E:\BotDevelopment\Rxiqi\status\ready.js:62:38)
    at emitTwo (events.js:126:13)
    at FSWatcher.emit (events.js:214:7)
    at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)

非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您正在使用什么操作系统?如果您在Windows上,则可能会出现问题,因为您的“ /”无法正常工作