[TOKEN_INVALID]: An invalid token was provided

Viewed 15854

I'm trying to host a discord bot on Heroku.

When it starts I get this error:

"UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided".

But here is the weird part. It worked fine when I tested it first in Visual Studio Code. So in the process of me getting the files into Heroku, it got invalid.

Does anyone have a clue of how I can fix this?

Thx for any replies in advance.

5 Answers

Deploying app on heroku you have to change you client.login(token) to client.login(process.env.token) and add token to enter image description here as token - key and value - token copied from discord developers portal, remember (important note!) letters has to be the same, I mean, if you use capitals in process.env.TOKEN, the config var has to be now TOKEN.

I run into a similar problem, i encrypted the token so that i could store the encrypted token on heroku and decrypt it in the js to pass the actual token to the login method. The weird part is, if i write the token directly as string into the js file or i use heroku and store it directly there, then it works. But if i store the encrypted token in heroku and decrypt it (and yes it is correctly decrypted) and give the decrypted token to the login method then it does not work. I dunno if heroku make some weird stuff or discord.js doesnt support something i dont know...

So, what definitly works is: You insert the token (client secret) on herokus config vars (settings from your dyno), then you use process.env.BOT_TOKEN and pass it to your Discord.Client().login(). I assume in your case its bot.login(process.env.BOT_TOKEN).

So for me it looks like the value i store in heruko and the value i send to discord must be the same...

You should enter a valid Discord bot token. You can obtain it by going to the Discord Developer Portal, then "Applications", select your bot, then go to "Bot" and click "Copy" under the token (it says click here to reveal).

I also encountered this situation.

I discovered that when using dotenv to read data from a .env file (Run on my computer), the names are not case sensitive. (could be wrong)

For example, TOKEN will be able to be treated as token, meaning when process.env.token can be returned with the value of TOKEN in the .env file

But when I run on Heroku, what I just said will be gone (process.env.token will not return the value of TOKEN, but the token). Try double-checking that the name matches and correct it.

Like you've said, you logged into your bot with bot.login('TOKEN', () => { console.log; }

but bot.login doesn't support callback functions.

What I mean by this is, instead of the code before, you change it to bot.login('TOKEN').

Related