I'm creating a discord bot, and trying to host it in Heroku. After installing all dependencies I tried to test before deploying but the following error appears:

[email protected]:~/Documents/DMGBOT$ heroku local
3:00:24 AM worker.1 |  > [email protected] start /home/nicolasperez/Documents/DMGBOT
3:00:24 AM worker.1 |  > node bot.js
3:00:25 AM worker.1 |  (node:7624) UnhandledPromiseRejectionWarning: Error: An invalid token was provided.
3:00:25 AM worker.1 |      at Promise (/home/nicolasperez/Documents/DMGBOT/node_modules/discord.js/src/client/rest/RESTMethods.js:33:44)
3:00:25 AM worker.1 |      at new Promise (<anonymous>)
3:00:25 AM worker.1 |      at RESTMethods.login (/home/nicolasperez/Documents/DMGBOT/node_modules/discord.js/src/client/rest/RESTMethods.js:32:12)
3:00:25 AM worker.1 |      at Client.login (/home/nicolasperez/Documents/DMGBOT/node_modules/discord.js/src/client/Client.js:277:30)
3:00:25 AM worker.1 |      at Object.<anonymous> (/home/nicolasperez/Documents/DMGBOT/bot.js:194:5)
3:00:25 AM worker.1 |      at Module._compile (module.js:652:30)
3:00:25 AM worker.1 |      at Object.Module._extensions..js (module.js:663:10)
3:00:25 AM worker.1 |      at Module.load (module.js:565:32)
3:00:25 AM worker.1 |      at tryModuleLoad (module.js:505:12)
3:00:25 AM worker.1 |      at Function.Module._load (module.js:497:3)
3:00:25 AM worker.1 |      at Function.Module.runMain (module.js:693:10)
3:00:25 AM worker.1 |      at startup (bootstrap_node.js:188:16)
3:00:25 AM worker.1 |      at bootstrap_node.js:609:3
3:00:25 AM worker.1 |  (node:7624) 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(). (rejection id: 1)
3:00:25 AM worker.1 |  (node:7624) [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.
[DONE] Killing all processes with signal  SIGINT
3:00:25 AM worker.1 Exited Successfully

I don't understand why this happens, as I have already set the config variables in the app settings in Heroku.

Here is the code for the bot: the important line is the last one, where the bot tries to log in with a valid token.

const botconfig = require("./botconfig.json");
const Discord = require("discord.js");
const Gamedig = require('gamedig');
const bot = new Discord.Client({disableEveryone: true});

bot.on("ready", async message => {...});

bot.on("message", async message => {...});

bot.login(process.env.BOT_TOKEN);

I have tried resetting the bot token numerous times, surrounding them with quotation marks, and including a .json with the token, but nothing has worked so far.
Why don't the config vars work knowing that they're set to a valid discord bot token? What other solutions can help my bot work without issues?

1 Answers

0
Federico Grandi On

The heroku local command makes the app run on your pc: that means that the environment variables that appear in process.env are then ones of your pc, not the ones you set in Heroku.

In order to load your Heroku variables into the local instance, you'll need to use a .env file: that file will be read by Heroku and it will insert those variables into the environment. You can find more info about that in "Running Apps Locally" by Heroku.
There's also a guide on how to copy existing variables to a .env file in the "Copy Heroku config vars to your local .env file" section.