Node.js “node bot.js” 命令未运行(正在构建 Discord 机器人)。

huangapple go评论85阅读模式
英文:

Node.js "node bot.js" command not running (building a discord bot)

问题

以下是您提供的代码的翻译部分:

require('dotenv').config();
const Discord = require("discord.js");
const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
    if (msg.content === "ping") {
        msg.reply("pong");
  } 
}) 
client.login(process.env.DISCORD_TOKEN);

请告诉我您需要什么进一步的帮助。

英文:

Alright, so I followed a small tutorial on how to make a discord bot and in the end I got this code:

require('dotenv').config();
const Discord = require("discord.js");
const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
    if (msg.content === "ping") {
        msg.reply("pong");
  } 
}) 
client.login(process.env.DISCORD_TOKEN);

Basically the bot must reply with "Pong" every time someone says "Ping"

After that, the tutorial said that to get the bot online I'd need to run the "node bot.js" command on a terminal, and I did, but the output was clearly an error and not what was shown in the tutorial (also the bot didn't go online)

throw new DiscordjsRangeError(ErrorCodes.BitFieldInvalid, bit);
    ^

RangeError [BitFieldInvalid]: Invalid bitfield flag or number: GUILDS.
    at IntentsBitField.resolve (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:172:11)
    at C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:167:54
    at Array.map (<anonymous>)
    at IntentsBitField.resolve (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:167:40)      
    at new BitField (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\BitField.js:33:38)
    at new IntentsBitField (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\util\IntentsBitField.js:9:1)      
    at Client._validateOptions (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\client\Client.js:494:25)      
    at new Client (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\node_modules\discord.js\src\client\Client.js:78:10)
    at Object.<anonymous> (C:\Users\pedro\OneDrive\Ambiente de Trabalho\Nova pasta\bot.js:3:16)
    at Module._compile (node:internal/modules/cjs/loader:1254:14) {
  code: 'BitFieldInvalid'
}

Node.js v18.16.0

I am very confused and I'm sorry if for some this may look like a stupid question and/or mistake, but I just don't know what to do :/

Here is the tutorial link:
https://www.xda-developers.com/how-to-create-discord-bot/

答案1

得分: 0

The error is: 无效的位字段标志或数字:GUILDS。

看起来 GUILDS 不是意图的预期值。

阅读文档此处,它们展示了如何添加意图。

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
    ],
});

对于你:

const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
// 变成
const client = new Discord.Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});
英文:

The error is: Invalid bitfield flag or number: GUILDS.

It seems like GUILDS is not the expected value for intents.

Reading the doc here they show how to add intents.

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
	intents: [
		GatewayIntentBits.Guilds,
		GatewayIntentBits.GuildMessages,
		GatewayIntentBits.MessageContent,
		GatewayIntentBits.GuildMembers,
	],
});

So for you:

const client = new Discord.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
//to
const client = new Discord.Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});

答案2

得分: 0

I think changing this.

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

To this should fix it.

const client = new Discord.Client({intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent]});

Also be sure to enable these:

Node.js “node bot.js” 命令未运行(正在构建 Discord 机器人)。

Edit: Also noticed the event is set on message, should be messageCreate.

英文:

I think changing this.

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

To this should fix it

const client = new Discord.Client({intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent]});

Also be sure to enable these:
Node.js “node bot.js” 命令未运行(正在构建 Discord 机器人)。

Edit: Also noticed the event is set on message, should be messageCreate

huangapple
  • 本文由 发表于 2023年6月15日 15:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定