Ask coding questions

← Back to all posts
Token error
TheIslandRhythm (0)

when I start my discord bot, I get an unhandled promise rejection token error but the bot token is stored in the env file where I call it correctly in the index file and there are no spaces in the env file so I don't understand how this is happening. The bot works fine when I run it locally but its only when I run it on repl that there is a problem. Has anyone experienced this problem before?

UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.
at CommandoClient.login (/home/runner/Beat-Bot/node_modules/discord.js/src/client/Client.js:205:52)

Upon further investigation, when I try to console.log the env variables so console.log(process.env.VARIABLE) they all return undefined.

Answered by Coder100 (18089) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18089)

Create a .env file. Inside, put:

TOKEN=[token here]

so if my token was abc it becomes:

TOKEN=abc
TheIslandRhythm (0)

@Coder100 I've done that and they return undefined

Coder100 (18089)

is it ok if you invite me to your repl @TheIslandRhythm

TheIslandRhythm (0)

@Coder100 I appreciate the offer but no thank you. If you have any specific questions about the bot to help I'll gladly respond

Coder100 (18089)

hm
how does your .env file look like? Because that's where the error is @TheIslandRhythm

TheIslandRhythm (0)

@Coder100 TOKEN=value -> newline -> PREFIX=!? -> newline -> OWNERID=othervalue \value and othervalue are substitutes for what they actually are

TheIslandRhythm (0)

@Coder100 it hard to format in the comments here so thats indicating what next so after first line a newline with the second variable ...

Coder100 (18089)

@TheIslandRhythm so no .env files work.
hm.

I guess you will have to report this to bugs with the given information you have.

TheIslandRhythm (0)

@Coder100 so essentially here's whats happening. If i type node . in the terminal its a no go. If I click the run button at the top which runs npm start which runs node . , it works

Janatbek (0)

can't post an answer, but comment is fine.
so with js bot, you can't create .env file anymore, use secrets tab and create a variable called TOKEN and paste the token in value field. save and run your repl.

RohilPatel (1581)

You are probably using the client id!! Go to the portal > bot > then get the token, its really long. Mark as answered if this helps.

TheIslandRhythm (0)

@RohilPatel I double checked, I am using the bot token

RohilPatel (1581)

Ur env file should look like this:

TOKEN=474mrifirm48

No spaces, nothing
@TheIslandRhythm

HahaYes (1861)

it looks like it works?

TheIslandRhythm (0)

@HahaYes I changed the repl file run back to npm start which just does node . but when I manually type it I get the error

HahaYes (1861)

@TheIslandRhythm huh yeah I don't know what happened

HahaYes (1861)

try run = "npm install" I'm not good with js

HahaYes (1861)

ah... did you import this from github?

HahaYes (1861)

@TheIslandRhythm in the .replit file try npm install tell me if it works

TheIslandRhythm (0)

@HahaYes it didn't. I also updated the post to include the error message