简体   繁体   English

为什么我会收到 ReferenceError: AbortController is not defined in Discord.js v13?

[英]Why am I getting a ReferenceError: AbortController is not defined in Discord.js v13?

Today (7.8.2021) Discord.js v13 has been released.今天(7.8.2021) Discord.js v13已经发布。 So I upgraded my previous Discord.js installation with所以我升级了我以前的Discord.js安装

npm i discord.js@latest

and then adapted my basic index.js file to this state (I followed the Discord.js Guide ):然后将我的基本index.js文件调整为这种状态(我遵循了Discord.js 指南):

const { Client, Intents } = require("discord.js");

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

client.once("ready", () => {
  console.log("Ready!");
});

client.login("my-token");

However when I try to run my code I keep getting this error:但是,当我尝试运行我的代码时,我不断收到此错误:

$ node . $节点。

(node:11216) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined (node:11216) UnhandledPromiseRejectionWarning: ReferenceError: AbortController 未定义

at RequestHandler.execute (C:\\ pathTo \\node_modules\\discord.js\\src\\rest\\RequestHandler.js:172:15)在 RequestHandler.execute (C:\\ pathTo \\ node_modules \\discord.js\\src\\rest\\RequestHandler.js:172:15)

I've been working with Discord.js since 2018, hence I was astonished that I was having trouble with the new release.自 2018 年以来,我一直在使用Discord.js ,因此我很惊讶我在新版本中遇到了问题。 Is this an issue with the Discord.js v13 library?这是Discord.js v13库的问题吗? If not how can I fix the error?如果不是,我该如何修复错误?

The Issue:问题:

One of the prerequisites for using Discord.js v13 is that you need to use NodeJS v16.6 or higher (emphasis mine):使用 Discord.js v13 的先决条件之一是您需要使用 NodeJS v16.6 或更高版本(重点是我的):

v13 requires Node 16.6 or higher to use, so make sure you're up to date. v13 需要使用 Node 16.6 或更高版本,因此请确保您是最新的。 To check your Node version, use node -v要检查您的节点版本,请使用 node -v


The Fix:修复:

The fix is to update your node version, you can confirm your current node version by running node -v .修复是更新您的节点版本,您可以通过运行node -v来确认您当前的节点版本。 There are a variety of different ways to update node, one way is to run the following commands if you're using Linux / iOS:多种不同的方式来更新节点,如果您使用的是 Linux / iOS,一种方式是运行以下命令:

> npm install -g n
> n latest

The first command installs the command-line interface n , and then the second uses n to update to the latest stable version of node.第一个命令安装命令行界面n ,然后第二个命令使用n更新到最新的稳定版本的节点。 If you're on Windows, you can install NVM as outlined in this answer by pradeek .如果您使用的是 Windows,则可以按照pradeek 的此答案中所述安装NVM

Note for Heroku users : Heroku 用户注意事项

Once you have followed the above steps to update your node version, you might then be required to update your package.json file by adding/updating the engines:按照上述步骤更新节点版本后,您可能需要通过添加/更新引擎来更新package.json文件:

"engines": {
  "node": "16.x"
}

Why this broke in v13?:为什么这在 v13 中崩溃了?:

A few versions ago of discordjs, a feature was added that aborts requests that take too long (longer than 15 seconds).在 discordjs 的几个版本之前,添加了一项功能,可以中止耗时过长(超过 15 秒)的请求。 In order to achieve this functionality, they were previously using the node package abort-controller .为了实现这个功能,他们之前使用了节点包abort-controller However, now that nodejs has evolved, it now has its own AbortController global without the need to require an external package.然而,现在 nodejs 已经进化,它现在有了自己的AbortController全局,不需要外部包。 Discord.js v13 now relies on this global as they're no longer using the abort controller package . Discord.js v13 现在依赖于这个全局变量,因为它们不再使用 abort 控制器包 In order to use the AbortController global provided by nodejs, you need to be using node v15 or higher - however, as recommended by the discord.js guide, you should be using v16.6+ to enable support for other features it may use.为了使用AbortController提供的AbortController全局,您需要使用 node v15 或更高版本 - 但是,根据 discord.js 指南的建议,您应该使用 v16.6+ 以启用对它可能使用的其他功能的支持。

If you don't want to install node.js v16 in all your system, and just add it in your bot's project, you can do:如果你不想在你的所有系统中安装 node.js v16,而只是将它添加到你的机器人项目中,你可以这样做:

npm install node@16.6.1 --save-exact

And it will run.它会运行。

N, the updater used in the solution, doesn't work for Windows operating systems. N,解决方案中使用的更新程序,不适用于 Windows 操作系统。 If you use Windows there is still a way to update.如果您使用 Windows,仍有一种方法可以更新。 Go to the Node.JS installation website转到Node.JS 安装网站

If you go there manually without clicking the link I provided, you will need to switch from the LTS option to Current.如果您在没有单击我提供的链接的情况下手动访问,则需要从 LTS 选项切换到 Current。如这里所见

^ as seen here ^ 如这里所见

Choose the MSI installer and the architecture of which your old Node.JS version was installed with.选择 MSI 安装程序和旧 Node.JS 版本所安装的架构。

If you don't know the architecture of which your old Node.JS version was installed with you can check by checking if the nodejs folder is in "C:/Program Files" or "C:/Program Files (x86)"如果您不知道旧 Node.JS 版本所安装的架构,您可以通过检查 nodejs 文件夹是否在“C:/Program Files”或“C:/Program Files (x86)”中来检查

Then just install Node.JS as if it was the first time.然后就像第一次一样安装 Node.JS。

Enjoy!享受!

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

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