【问题标题】:Discord bot on Heroku throws an error: agent ??= new https.AgentHeroku 上的 Discord 机器人抛出错误:agent ??= new https.Agent
【发布时间】:2022-01-25 06:39:12
【问题描述】:

这与我的第一个问题有关。我更新了所有代码。在我的bot.js 中是:

require('dotenv').config();

let ver = process.env.DISCORD_BOT;

client.once('ready', async () => {
  if (ver === 'production') {
    client.user.setActivity(`in code land`, { type: 'PLAYING' });
  } else {
    client.user.setActivity(`over ${client.guilds.cache.size} server(s)`, {
      type: 'WATCHING',
      status: 'IDLE',
    });
  }
  console.log(`Logged in as ${client.user.tag}!`);
  console.log(`the prefix is ` + prefix);
});

Procfile:

worker: node bot.js

package.json:

 "scripts": {
    "start": "node .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "production": "NODE_ENV=production&&npm start",
    "development": "set NODE_ENV=development&&npm start"
  }

.env:

DISCORD_BOT= TOKEN

当我检查 Heroku 应用程序日志时,抛出的错误如下:

2021-12-22T03:13:05.436081+00:00 app[worker.1]: 代理 ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: 真 });

2021-12-22T03:13:05.436082+00:00 应用[worker.1]: ^^^

2021-12-22T03:13:05.436082+00:00 应用[worker.1]:

2021-12-22T03:13:05.436082+00:00 app[worker.1]: SyntaxError: 意外的令牌'??='

我尝试搜索 "SyntaxError: Unexpected token '??='" 并且每个结果都建议将 node.js 更新到 v16-17,但我已经在使用Discord.js 和 Node.js 的最新版本。

【问题讨论】:

  • 如果您对代码进行了更改或有其他信息,请将其添加到您现有的问题中,不要为其创建新问题:)

标签: javascript node.js heroku discord.js


【解决方案1】:

错误来自 discord.js,这是因为您使用的是旧版本的 Node.js。 logical nullish assignment operator (??=) 只是 available in node v15+。

您可能认为您使用的是最新版本的 Node.js,但 Heroku 表示 “如果引擎中未指定 Node 版本,将使用 14.x 版本”时间>。您可以将 engines 属性添加到您的 package.json 文件中以指定您需要的版本。作为 discord.js v13 requires node.js v16.6+,您可以添加以下内容:

  "engines": {
    "node": "16.6"
  }

或者请求最新的 v16,添加这个:

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

【讨论】:

  • 在package.json 中添加engines 后,SyntaxError: Unexpected token '??=' 消失了。但它总是说State change from up to crashed。
  • 上次更新。最后一个错误在我的代码中。但最后!完成!谢谢大家!
猜你喜欢
  • 2018-10-10
  • 2020-08-06
  • 2019-01-12
  • 2021-12-04
  • 2019-10-31
  • 2021-08-14
  • 2022-01-23
  • 2021-08-17
  • 2018-07-24
相关资源
最近更新 更多