我正在执行 ping pong 命令,但我的代码出现错误
I am making a ping pong command and I am getting an error in 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);
这是我得到的错误:
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'
这是我的 settings.json
文件:
{
"clientId": "123456789012345678",
"guildId": "876543210987654321",
"token": "xxxxxxxxxxxxxxxxxxxxx"
}
您需要使用 client
,它是您在第 4 行创建的 Client
class 的实例,而不是 Client
,它是 class本身。
您的第 11 行应如下所示:client.on('message', msg => {
和最后一行的 client.login(token);
相同。
尝试像这样对您的令牌使用导入:
const { token } = require('./settings.json')
我正在制作一个机器人,但我在第 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);
这是我得到的错误:
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'
这是我的 settings.json
文件:
{
"clientId": "123456789012345678",
"guildId": "876543210987654321",
"token": "xxxxxxxxxxxxxxxxxxxxx"
}
您需要使用 client
,它是您在第 4 行创建的 Client
class 的实例,而不是 Client
,它是 class本身。
您的第 11 行应如下所示:client.on('message', msg => {
和最后一行的 client.login(token);
相同。
尝试像这样对您的令牌使用导入:
const { token } = require('./settings.json')