Discord.js-TypeError:commandfile.run不是函数

时间:2019-07-17 16:36:54

标签: javascript node.js discord discord.js

由于这个错误,我做了ytdl Discord Bot和?play。这是我的代码:

if(!message.content.startsWith(prefix)) return;
    let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))
    if (commandfile) commandfile.run(bot, message, args);

再次,可能是很容易解决的问题,但是我似乎在这里找不到问题,因为它以前曾起作用。如果我又搞砸了,那就开玩笑

我的play.js文件:https://pastebin.com/4B0DVjWC

1 个答案:

答案 0 :(得分:0)

在播放命令文件中,您要将新的侦听器添加到新客户端的message事件中。其中的代码仅在发出事件时才运行,而不会发生。因此,module.exports.run永远不会被声明。这意味着您其他文件中的require()返回的对象没有run属性。当您尝试调用不存在的函数时,将引发错误。

文件格式应如下所示:

const Discord = require('discord.js');
// Import other necessary libraries, require files, etc...

module.exports.run = async (bot, message, args) => {
  ...
};

module.exports.config = {
  ...
};

请注意,我不是添加其他侦听器,而是直接导出函数。

Documentation on modules