启动不和谐机器人时出现问题(它不想工作)

Problem launching a discord bot (which doesn't want to work)

第一次做真正的javascript项目(之前用过一点点js,仅此而已)。我正在尝试制作一个不和谐的机器人。我将它托管在 heroku 上,但在部署我的项目时出现无法解决的错误。这是日志中的内容:

2021-08-20T13:01:23.972825+00:00 heroku[worker.1]: Starting process with command `node index.js`

2021-08-20T13:01:23.771024+00:00 heroku[web.1]: Starting process with command `npm start`

2021-08-20T13:01:24.646055+00:00 heroku[worker.1]: State changed from starting to up

2021-08-20T13:01:27.027743+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: ReferenceError: AbortController is not defined

2021-08-20T13:01:27.027754+00:00 app[worker.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:172:15)

2021-08-20T13:01:27.027754+00:00 app[worker.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:176:19)

2021-08-20T13:01:27.027755+00:00 app[worker.1]:     at RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:25)

2021-08-20T13:01:27.027755+00:00 app[worker.1]:     at async WebSocketManager.connect (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:128:9)

2021-08-20T13:01:27.027755+00:00 app[worker.1]:     at async Client.login (/app/node_modules/discord.js/src/client/Client.js:245:7)

2021-08-20T13:01:27.027756+00:00 app[worker.1]: (Use `node --trace-warnings ...` to show where the warning was created)

2021-08-20T13:01:27.028065+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

2021-08-20T13:01:27.028108+00:00 app[worker.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我的电脑上已经有这个错误了,我刚刚升级了node版本就没有了。但是,我不知道如何处理 heroku。此外,当我在我的计算机上修复错误时,它似乎可以正常工作,但是当我输入“!ping”时,机器人没有回答我,不和谐。

所以我有两个问题:

  1. 'UnhandledPromiseRejectionWarning',在 heroku
  2. 我的机器人不工作

有人可以帮助我。

以下是版本:

node : v16.7.0
discord.js : v13.1.0

这是我的代码:

index.js

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

const prefixCmd = '!';

client.on("ready", () => {
    console.log("I'm ready !");
});

client.on("message", msg => {

    if(!msg.content.startsWith(prefixCmd) || msg.author.bot) return

    const args = msg.content.slice(prefixCmd.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === "ping") {
        msg.reply("pong");
    }
});

client.login("MY TOKEN");

package.json

{
  "name": "ha",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "discord.js": "^13.1.0",
    "node": "^16.6.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/......"
  },
  "author": "",
  "license": "ISC"
}

要解决此问题,您必须添加:

"engines": {
    "node": "16.7.0" 
}

到package.json

要使 heroku 正常工作,您需要从 package.json 中的依赖项中删除节点。

并在 package.json 中添加:

"engines": {
    "node": "16.7.0" 
}

对于 运行 的机器人,您不能写:

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

但是你必须写:

const { Client, Intents } = require('discord.js');
const client = new Client({
    intents: [
        "GUILDS",
        "GUILD_MESSAGES",
        "DIRECT_MESSAGES"
    ],
    partials: [
    "CHANNEL"
    ]
});