简体   繁体   English

我正在制作一个乒乓球命令,但我的代码中出现错误

[英]I am making a ping pong command and I am getting an error in my code

I'm making a bot and I'm getting an error saying that TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an instance of EventEmitter. Received type string ('message')我正在制作一个机器人,但收到一条错误消息,提示TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an instance of EventEmitter. Received type string ('message') TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an instance of EventEmitter. Received type string ('message') on line 11. Here's my code:在第 11 行TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an instance of EventEmitter. Received type string ('message') 。这是我的代码:

const { Client, Intents } = require("discord.js");
const settings = require("./settings.json");

const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS, 
    Intents.FLAGS.GUILD_MESSAGES
  ]
});

Client.on("message", (msg) => {
  // Message function
  if (msg.author.bot) return; // Ignore all bots
  if (msg.content.startsWith(settings.prefix)) return; // It always has to starts with the prefix which is '!'

  if (msg.content.startsWith(settings.prefix + "ping")) {
    // When a player does '!ping'
    msg.reply("Pong!"); // The bot will say @Author, Pong!
  }
});

Client.login(token);

This is the error I get:这是我得到的错误:

throw new ERR_INVALID_ARG_TYPE('emitter', 'EventEmitter', emitter);
        ^    
TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an instance of EventEmitter. Received type string ('message')
    at new NodeError (node:internal/errors:372:5)
    at eventTargetAgnosticAddListener (node:events:1008:11)
    at Function.on (node:events:1095:3)
    at Object.<anonymous> (C:\Users\yybro\discordBot\dex.js:11:8)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
      code: 'ERR_INVALID_ARG_TYPE' 

And this is my settings.json file:这是我的settings.json文件:

{
    "clientId": "123456789012345678",
    "guildId": "876543210987654321",
    "token": "xxxxxxxxxxxxxxxxxxxxx"
}

You need to use client which is the instance of the Client class you made on line 4, and not Client which is the class itself.您需要使用client ,它是您在第 4 行创建的Client类的实例,而不是Client ,它是类本身。

Your line 11 should look like that: client.on('message', msg => { and same for client.login(token); on the last line.您的第 11 行应如下所示: client.on('message', msg => {和最后一行的client.login(token);相同。

尝试像这样为您的令牌使用导入:

const { token } = require('./settings.json')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的代码中出现错误 - Why am I getting a error in my code 为什么我的 Join DM 命令出现错误? - Why am I getting an error on my Join DM command? 为什么我的 Route Redux 代码中出现错误? - Why am I getting an error in my Route Redux code? 为什么在运行我的代码时会收到错误“UnhandledPromiseRejectionWarning”? - Why am I getting error "UnhandledPromiseRejectionWarning" when running my code? 如果我已经重新连接 onclose(),为什么我需要 pingpong 来检测 websocket 连接断开? - Why do I need ping-pong to detect websocket connection drops if I am already reconnecting onclose()? 我在我的js代码中缺少的东西我正在丢失)错误 - What i am missing in my js code i am getting missing ) error 我在代码中添加了框架标签,然后出现此错误? - I am add frame tag in my code then I am getting this error? 我在magento网站中收到错误消息 - I am getting the error in my magento site 我正在尝试使用延迟执行 discord.js ping 命令,但它显示此错误 - I am trying to make a discord.js ping command with latency but it shows this error 我是一个初学者 js 编码器,试图与计算机对手进行乒乓球比赛,但球直接穿过桨 - I am a beginner js coder trying to make a ping pong game against a computer opponent, but the ball goes right through the paddle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM