Discord.js错误-找不到模块

时间:2018-08-10 16:18:01

标签: node.js module compiler-errors discord.js

我正在使用Discord.js为Discord编程一个机器人。该机器人在过去的一个月中一直运行良好,过去几天我对代码所做的唯一更新是更新Discord.js和Enmap。但是,现在,当我尝试运行命令(在本示例中为您提供帮助)时,出现此错误:

Error: Cannot find module './commands/help.js'

at Function.Module._resolveFilename (module.js:547:15)

at Function.Module._load (module.js:474:25)

at Module.require (module.js:596:17)

at require (internal/module.js:11:18)

at module.exports (/app/events/message.js:162:17)

at emitOne (events.js:116:13)

at Client.emit (events.js:211:7)

at MessageCreateHandler.handle (/rbd/pnpm-volume/6c985a92-4a9b-4fa1- 
   bea8-7452f0bbbcd9/node_modules/.registry.npmjs.org/discord.js/11.4.0/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)

at WebSocketPacketManager.handle (/rbd/pnpm-volume/6c985a92-4a9b-4fa1- bea8-7452f0bbbcd9/node_modules/.registry.npmjs.org/discord.js/11.4.0/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:103:65)

这是我的命令处理程序的外观(在我的主要bot文件中):

client.commands = new Enmap();

fs.readdir("commands/", (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    if (!file.endsWith(".js")) return;
    let command = `./commands/${file}`;
    let commandName = file.split(".")[0];
    client.commands.set(commandName, command);
    console.log(`Loaded ${commandName}`);
  });
});

这就是事件message.js的外观(此处包括冷却功能):

const args = msg.content.slice(prefix.length).trim().split(/ +/g);
console.log(args);
const command = args.shift().toLowerCase();
console.log(command);
const cmd = require(client.commands.get(command));
if (!cmd || cmd === null) return;
else {
  var time = Date.now();
  if (client.cd.has(msg.author.id)) {
    var wait = client.cd.get(msg.author.id);
    if (time < wait) {
      msg.channel.send(new client.discord.RichEmbed().setColor(client.color).setDescription(`Please wait **${Math.ceil((wait - time)/1000)}** seconds before using another command!`)).then(msg => {msg.delete(2000).then(()=>{console.log("deleted")}).catch(error=> {console.error(error)})});
    }
    else {
      client.cd.set(msg.author.id, Date.now() + (client.sec * 1000));
      try {
        cmd.run(client, msg, args);
      }
      catch (err) {
        console.error(err);
      }
    }
  }
  else {
    client.cd.set(msg.author.id, Date.now() + (client.sec * 1000));
    try {
      cmd.run(client, msg, args);
    }
    catch (err) {
      console.error(err);
    }
  }
}

我所有的命令文件也都以exports.run = (client, msg, args) =>开头

Node.js / Discord.js是否有理由停止读取我的命令文件?谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

没关系,我知道是什么引起了问题。当我的命令文件夹中的所有命令文件都应该以{{1​​}}开头时,它的所有命令文件都以exports.run = (client, msg, args) =>开头。这样,Node.js就能将module.exports = { name: 'command-name', run(client, msg, args) {识别为一个函数。

答案 1 :(得分:0)

client.commands = new Discord.Client();

fs.readdir("/commands/", (err, files) => {
  if (err) throw err;
  let jsFile = files.filter(f => f.endsWith(".js");
  if(jsFile.length <= 0) return console.log("No JS Files!")
  files.forEach(file => {
    if (!file.endsWith(".js")) return;
    let props = require(`./commands/${file}`);
    console.log(`Loading ${file}`)

    client.commands.set(props.help.name, props);
  });
});

您应该在{commands“之前添加/ 这只是我添加的一些代码建议。