0

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"
}
MC001
  • 1
  • 5
  • Are you sure the node version is v16? – user15517071 Aug 21 '21 at 01:37
  • @user15517071 Yes, I'm sure. When I write 'node -v' in the terminal, I get: v16.7.0 – MC001 Aug 21 '21 at 13:44
  • Hi, that problem is originated when using Discord.js V13 with older Node versions. First of all, node should not be set in the dependecies so try removing that and runing it again. Tell me if that not works. Also, do you have the same problem when running it locally? Or is it just on Heroku? – Nico Halpe Aug 21 '21 at 19:14
  • Hi @Nico, I just removed node from the dependencies in the json file. But I still have the problem. – MC001 Aug 21 '21 at 21:42
  • @Nico, For the problem of 'UnhandledPromiseRejectionWarning' I already had it on my computer. I had version 14 of node, when I switched to 16 I didn't have the problem anymore. But my bot wasn't working, I searched everywhere and I couldn't find why. That's why I went to heroku, because I thought that the problem might come from the configuration of my computer (or router), especially with the port opening. But on heroku, I get the problem : 'UnhandledPromiseRejectionWarning', and I don't know how to fix it, especially because in my json it was specified that I want version 16 of node. – MC001 Aug 21 '21 at 21:43
  • @MC001 Maybe try creating a new heroku app with the node js buildpack and see if it works – Nico Halpe Aug 21 '21 at 23:27
  • @Nico, sorry but it doesn't work – MC001 Aug 21 '21 at 23:46
  • @MC001 I think I have the solution, try adding: "engines": { "node": "16.7.0" } to your package.json – Nico Halpe Aug 22 '21 at 00:30
  • @Nico, thank you very much, I no longer have the error. But my bot doesn't want to work, do you know why? – MC001 Aug 22 '21 at 00:43
  • @MC001 I will post that as an answer and check why is it not working – Nico Halpe Aug 22 '21 at 00:50
  • Try making a console.log(msg) in the event. Btw, can you upvote and check the answer as correct?, so if another one have the same problem can check this. – Nico Halpe Aug 22 '21 at 00:54
  • @Nico I try it, but when i write a message on discord, nothing is displayed in the terminal – MC001 Aug 22 '21 at 01:22
  • @Nico, I get the 'I'm ready !' in the terminal, but nothing when I write a message in discord (even with the 'console.log(msg)'). I think of 2 possibilities: 1. The problem must come from 'client.on("message", msg => {...});', but I can't find anything on it. 2. my bot is not linked to the discord server, but I have integrated it well (normally) and I put the token. Do you have an idea ? – MC001 Aug 22 '21 at 18:14
  • @Nico, I just wrote : ```client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`); client.user.setActivity('You', {type: 'WATCHING'}); const channel = client.channels.cache.find(channel => channel.id === '7725373434xxxxxxxx'); if (!channel) return; channel.send("Hello, world!"); });``` to find out if my second guess was right or not. I did receive "Hello, world!" on discord, so my second guess is wrong. – MC001 Aug 23 '21 at 00:45
  • I just found : i'va to write : ```const { Client, Intents } = require('discord.js'); const client = new Client({intents: ["GUILDS", "GUILD_MESSAGES", "DIRECT_MESSAGES"], partials: ["CHANNEL"]});``` – MC001 Aug 23 '21 at 01:03
  • @MC001 nice, so you solved it right? This new Intents are anoying – Nico Halpe Aug 23 '21 at 02:56
  • Yes, it is anoying – MC001 Aug 23 '21 at 14:00

2 Answers2

0

To solve this issue, you will have to add:

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

To the package.json

Nico Halpe
  • 398
  • 2
  • 9
0

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"
    ]
});
MC001
  • 1
  • 5