简体   繁体   English

类型错误 [ClientMissingIntents] | Discord 机器人编码 | node.js / discord.js

[英]TypeError [ClientMissingIntents] | Discord Bot Coding | node.js / discord.js

Whenever I try to run my bot's code using node.每当我尝试使用node. , the following error message appears: ,出现以下错误消息:

TypeError [ClientMissingIntents]: Valid intents must be provided for the Client.
    at Client._validateOptions (D:\Neuer Ordner\node_modules\discord.js\src\client\Client.js:489:13)
    at new Client (D:\Neuer Ordner\node_modules\discord.js\src\client\Client.js:78:10)
    at Object.<anonymous> (D:\Neuer Ordner\index.js:2:13)
    at Module._compile (node:internal/modules/cjs/loader:1239:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
    at Module.load (node:internal/modules/cjs/loader:1096:32)
    at Module._load (node:internal/modules/cjs/loader:935:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:84:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'ClientMissingIntents'

My Code is:我的代码是:

const Discord = require("discord.js");
const bot = new Discord.Client();
const token =
  "";
const prefix = "t/";
bot.once("ready", () => {
  console.log("BOT WENT ONLINE");
});

bot.on("message", (message) => {
  if (message.author.bot) return;
  if (!message.content.startsWith("prefix")) return;
  if (message.content.startsWith(prefix + "ping")) {
    message.channel.send("PONG");
  }
  if (!message.content.startsWith(prefix + "ping")) {
    message.channel.send("WRONG COMMANDS!");
  }
});

bot.login(token);

I'm starting out with discord bot development, so I'm not sure what I can do to resolve this.我从 discord 机器人开发开始,所以我不确定我能做些什么来解决这个问题。

Why the error?为什么会出错?

Your code does not specify the intents that it needs to run when logging in, hence why it fails.您的代码没有指定登录时需要运行的意图,因此它失败了。 As stated in the discord.js guide :discord.js 指南中所述:

Gateway Intents were introduced by Discord so bot developers can choose which events their bot receives based on which data it needs to function. Intents are named groups of pre-defined WebSocket events, which the discord.js client will receive.网关意图由 Discord 引入,因此机器人开发人员可以根据其需要的数据来选择他们的机器人接收哪些事件 function。意图被命名为预定义的 WebSocket 事件组,discord.js 客户端将接收这些事件。 If you omit DirectMessageTyping, for example, you will no longer receive typing events from direct messages.例如,如果您省略 DirectMessageTyping,您将不再收到来自直接消息的键入事件。 If you do not specify intents, discord.js will throw an error.如果不指定意图,discord.js 将抛出错误。

How to fix this如何解决这个问题

Firstly, check your discord.js version using npm list .首先,使用npm list检查您的 discord.js 版本 The code you have written is for an older version of discord.js (v12.x), however based on the logs you've provided, the discord.js version you're using seems to be newer (v13.x or v14.x) and won't work with it.您编写的代码适用于 discord.js (v12.x) 的旧版本,但是根据您提供的日志,您使用的 discord.js 版本似乎较新(v13.x 或 v14.x)并且赢得了和它一起工作。

If your discord.js version is 12.x or below, then run npm remove discord.js then npm install discord.js to install the latest version of discord.js.如果您的 discord.js 版本是 12.x 或以下,则运行npm remove discord.js然后npm install discord.js安装最新版本的 discord.js。

I've included answers for versions 13.x and 14.x below:我在下面包含了版本 13.x 和 14.x 的答案:

The solution解决方案

Here is the baseline code you need for your bot to operate (source: discordjs.guide )这是您的机器人运行所需的基线代码(来源: discordjs.guide

Notice how at line 2 your bot's token isn't being stored in a variable, but rather we're now trying to access it from a config.json file.请注意,在第 2 行,您的机器人令牌并未存储在变量中,而是我们现在正尝试从config.json文件访问它。 It's generally a somewhat safer way of storing your token.它通常是一种更安全的存储令牌的方式。

Read the guide from here and follow the steps on configuring a config.json file to store your token a bit more safely, or preferrably install a package like dotenv ( npm install dotenv ) to handle your bot's secret keys instead.这里阅读指南并按照配置config.json文件的步骤更安全地存储您的令牌,或者最好安装 package 像dotenv ( npm install dotenv ) 来处理您的机器人的秘密密钥。

If you are using discord.js v14.x如果您使用的是 discord.js v14.x

Inside of your index.js file:在你的index.js文件中:

// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
    console.log(`Ready! Logged in as ${c.user.tag}`);
});

// Log in to Discord with your client's token
client.login(token);

If you are using discord.js v13.x如果您使用的是 discord.js v13.x

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

// Login to Discord with your client's token
client.login(token);

Both for discord.js v13.x and v14.x discord.js v13.x 和 v14.x

Inside your config.json file:在你的config.json文件中:

{
    "token": "your-token-goes-here"
}

What about commands?命令呢?

You may have noticed that the snippet above doesn't include any code for interacting with the bot.您可能已经注意到上面的代码片段不包含任何与机器人交互的代码。 Read below, this is very important:阅读下文,这非常重要:

If your bot is in under 100 servers and you do not plan on growing it, you can create text commands with a prefix, but it generally isn't the recommended way of interacting with bots anymore .如果您的机器人在 100 台以下的服务器中并且您不打算增加它,您可以创建带有前缀的文本命令,但通常不再推荐与机器人交互的方式 Because of data privacy restrictions, discord will prevent your bot from joining more than 100 servers if you are using the message content intent and haven't had it specifically approved by discord. Read more about this on discord's article .由于数据隐私限制,discord 将阻止您的机器人加入超过 100 个服务器,如果您正在使用消息内容意图并且没有得到 discord 的特别批准。阅读discord 的文章了解更多信息。

If you still want to read message content for text commands or moderation features, then add GatewayIntentBits.MessageContent and GatewayIntentBits.GuildMessages inside of the intents: [] array like so:如果您仍想阅读文本命令或审核功能的消息内容,请在intents: []数组中添加GatewayIntentBits.MessageContentGatewayIntentBits.GuildMessages ,如下所示:

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

...and listen for messages using the messageCreate event (place this below the client.login(token) line): ...并使用messageCreate事件侦听消息(将其放在client.login(token)行下方):

client.on("messageCreate", async (message) => {
    // Logs the message being sent by a user
    console.log(message);

    // add your prefix code here...
}

Read more on the guide on how to use slash commands (the newer, more intuitive and powerful way of interacting with Discord bots) here .此处阅读有关如何使用斜线命令(与 Discord 机器人交互的更新、更直观和更强大的方式)的指南的更多信息。

Please take some time to read the documentation or guide via the links above, or even watch a recent tutorial video if you'd like a more interactive way of coding your new discord bot.请花一些时间通过上面的链接阅读文档或指南,如果您想要一种更具交互性的方式来编写新的 discord 机器人,甚至可以观看最近的教程视频。

Best of luck on your bot coding adventures, I know it's a lot to take in, but give it some time and patience.祝你在机器人编码冒险中好运,我知道要接受很多东西,但请给它一些时间和耐心。 and you'll figure it out in no time.你很快就会弄明白的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM