Node.js“node bot.js”命令未运行(构建不和谐机器人)[重复]

gpfsuwkq  于 2023-06-22  发布在  Node.js
关注(0)|答案(2)|浏览(134)

此问题已在此处有答案

Discord.js v13 code breaks when upgrading to v14(1个答案)
6天前关闭
好吧,所以我遵循了一个关于如何制作不和谐机器人的小教程,最后我得到了这个代码:

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);

基本上,每次有人说“Ping”时,机器人都必须回复“Pong”
在那之后,教程说要让机器人在线,我需要在终端上运行“node bot.js”命令,我这样做了,但输出显然是一个错误,而不是教程中显示的内容(机器人也没有上线)

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

我很困惑,我很抱歉,如果对一些人来说,这可能看起来像一个愚蠢的问题和/或错误,但我只是不知道该怎么办:
以下是教程链接:https://www.xda-developers.com/how-to-create-discord-bot/

d7v8vwbk

d7v8vwbk1#

我想改变这个。

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

这个应该能修好

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

另外,请确保启用以下功能:

编辑:还注意到事件设置在message上,应该是messageCreate

gc0ot86w

gc0ot86w2#

错误是:无效的位域标志或数字:海盗。
似乎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"]});
//to
const client = new Discord.Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});

相关问题