agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });

agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });

所以我正在学习如何使用 discord.js

在 discord 上制作机器人

这是我的bot.js

require("dotenv").config();
const { Client } = require('discord.js');
const client = new Client();
client.login(process.env.DISCORD_TOKEN_KEY);

这是我的 package.json 文件:

{
  "name": "aibot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./src/bot.js",
    "dev": "nodemon ./src/bot.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^13.2.0",
    "dotenv": "^10.0.0"
  }
}

所以当我 运行 node ./src/bot.js 我收到此错误:

PS E:\RIYA\AIBOT> node -v
v14.18.0

PS E:\RIYA\AIBOT> node ./src/bot.js
E:\RIYA\AIBOT\node_modules\discord.js\src\rest\APIRequest.js:33
    agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
          ^^^

SyntaxError: Unexpected token '??='
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (E:\RIYA\AIBOT\node_modules\discord.js\src\rest\RESTManager.js:4:20)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

我该怎么办?我对 NodeJs 了解不多,我不知道如何解决这个问题。

您必须根据 the docs 将 node.js 更新到 v16.6 或更高版本(向下滚动到“安装”)。

您需要将您的节点更新到版本 16.6.0 或更高版本才能工作并修改您的

2nd and 3rd lines

代码为:

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

防止

[Symbol(code)]: 'CLIENT_MISSING_INTENTS' error

从文档中阅读更多内容 https://discord.js.org/#/docs/main/stable/general/welcome