【问题标题】:Undefined module when commands are grouped命令分组时未定义的模块
【发布时间】:2018-07-12 04:10:13
【问题描述】:

所以我正在做一个不和谐的机器人,并决定将我的命令分组在单独的文件中,例如 help.js 是一个命令,而 ping.js 是另一个命令。虽然现在的问题是命令找不到模块,所以代码给了我一个错误,比如:

UnhandledPromiseRejectionWarning: ReferenceError: ms is not defined

即使我在索引文件的顶部定义了它。 (ms 是我需要在静音命令中转换时间的模块)

const ms = require(“ms”); 

我用它来获取命令:

   try {
    if (fs.existsSync(`./commands/${command}.js`)) {

    let commandFile = require(`./commands/${command}.js`);

      commandFile.run(client, message, args);
    } else {
      message.reply(`+${command} does not exist`)
    }

这里是 ping 命令的例子:

exports.run = async (client, message, args, level) => { 
  const m = await message.channel.send("Ping?");
  m.edit(`Pong! Latency is ${m.createdTimestamp -message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
}

我已检查是否安装了节点模块。

我的问题是,我做错了什么,为什么会这样?我该怎么做才能解决这个问题?

【问题讨论】:

  • 你必须在你得到的每一个文件中导入/要求它。

标签: javascript node.js discord.js


【解决方案1】:

您需要在您拥有的每个文件中都需要它,除非它对您不可用。您也可以导出它,但您仍然需要导入主文件并从那里使用它。

【讨论】:

  • 是的,谢谢你的帮助。我还注意到你必须对函数做同样的事情。非常感谢您的帮助。
猜你喜欢
  • 2016-03-16
  • 2014-09-23
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多