【问题标题】:TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type FunctionTypeError [ERR_INVALID_ARG_TYPE]:“listener”参数必须是函数类型
【发布时间】:2019-01-24 15:41:33
【问题描述】:

我这里有个小问题:

events.js:200
抛出新的错误。ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
^
TypeError [ERR_INVALID_ARG_TYPE]:“listener”参数必须是类型 功能。接收类型未定义
在 _addListener (events.js:200:11)
在 Client.addListener (events.js:259:10)
在对象。 (D:\Yoshio\index.js:7:5)
在 Module._compile (internal/modules/cjs/loader.js:689:30)
在 Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
在 Module.load (internal/modules/cjs/loader.js:599:32)
在 tryModuleLoad (internal/modules/cjs/loader.js:538:12)
在 Function.Module._load (internal/modules/cjs/loader.js:530:3)
在 Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
启动时 (internal/bootstrap/node.js:266:19)

我搜索了答案,但找不到任何答案,请告诉我该怎么做。
这是我的代码:

const Discord = require("discord.js");

const TOKEN = "mytoken";

var bot = new Discord.Client();

bot.on("message"), function(message) {
  console.log(message.content);
};

bot.login(TOKEN);

【问题讨论】:

    标签: node.js visual-studio discord.js


    【解决方案1】:
    bot.on("message"), function(message) {
      console.log(message.content);
    };
    

    此处的错误表明您没有将回调函数传递给事件“消息”。

    这里的原因是语法错误,你在传递回调方法之前是右括号。

    解决方案:

    bot.on("message", function(message) {
      console.log(message.content);
    };
    

    【讨论】:

      【解决方案2】:

      应该像这样正确访问路径...

      const Discord = require("discord.js");
      

      //编辑,其实不应该这样。为您修复。

      【讨论】:

        【解决方案3】:

        从您提交的代码中,您将在将函数作为参数传递之前关闭您的on 调用。试试这个:

        const Discord = require("discord.js");
        
        const TOKEN = "mytoken"
        
        var bot = new Discord.Client();
        
        /*
         * Note the change here, the parenthesis is moved after
         * the function declaration so your anonymous function is now
         * passed as an argument.
         */
        bot.on("message", function(message) {
          console.log(message.content);
        });
        
        bot.login(TOKEN);
        

        【讨论】:

          猜你喜欢
          • 2020-09-16
          • 2020-10-31
          • 2020-04-16
          • 1970-01-01
          • 2020-08-29
          • 2021-12-24
          • 1970-01-01
          • 1970-01-01
          • 2019-10-01
          相关资源
          最近更新 更多