【问题标题】:Discord.js Error - Cannot Find ModuleDiscord.js 错误 - 找不到模块
【发布时间】:2019-01-18 07:15:49
【问题描述】:

我正在使用 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)

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

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) =&gt;开头

Node.js/Discord.js 是否有任何原因无法读取我的命令文件?感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    没关系,我知道是什么导致了问题。我的命令文件夹中的所有命令文件都以 exports.run = (client, msg, args) =&gt; 开头,而它应该以 module.exports = { name: 'command-name', run(client, msg, args) { 开头。这样,Node.js 将能够将 cmd.run(client, msg, args) 识别为一个函数。

    【讨论】:

      【解决方案2】:
      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);
        });
      });
      

      您应该在“命令”之前添加/ 这里只是我添加的一些代码建议。

      【讨论】:

      • new Enmapnew Discord.Client() 的更改将完全破坏他们的代码,因为这两个东西不一样,不能互换或兼容。
      猜你喜欢
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 2022-01-02
      • 2020-03-01
      • 2020-12-21
      • 2020-07-06
      • 2018-12-30
      • 2017-11-26
      相关资源
      最近更新 更多