Search code examples
javascriptnode.jsherokudiscord.js

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


This is the first time I'm doing a real javascript project (I've used js a little bit before, but that's all). I'm trying to make a bot for discord. I make it hosted on heroku, while deploying my project I get an error that I can't solve. Here is what is in the logs:

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.

I already had this error on my computer, I had just improve the version of node and I had no more error. However, I don't know what to do about heroku. Also, when I fixed the error on my computer it seemed to work, but when I typed '!ping' the bot didn't answer me, on discord.

So I have 2 problems:

  1. the 'UnhandledPromiseRejectionWarning', on heroku
  2. my bot doesn't work

someone could help me, please.

Here are the versions:

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

Here is my code:

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"
}

Solution

  • For heroku to work, you need to remove node from the dependencies in the package.json.

    And add, in the package.json :

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

    For the bot to run you must not write :

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

    But you have to write :

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