← Back to Community
Token error
Profile icon
TheIslandRhythm

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 [earned 5 cycles]
View Answer
Voters
Profile icon
TheIslandRhythm
Comments
hotnewtop
Profile icon
Coder100

Create a .env file. Inside, put:

TOKEN=[token here]

so if my token was abc it becomes:

TOKEN=abc
Profile icon
TheIslandRhythm

@Coder100 I've done that and they return undefined

Profile icon
Coder100
Profile icon
Coder100

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

Profile icon
TheIslandRhythm

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

Profile icon
Coder100

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

Profile icon
TheIslandRhythm

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

Profile icon
Coder100

@TheIslandRhythm coolio, but whats with ->?

Profile icon
TheIslandRhythm

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

Profile icon
Coder100
Profile icon
Coder100

@TheIslandRhythm so no .env files work.
hm.

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

Profile icon
TheIslandRhythm

@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

Profile icon
Coder100

@TheIslandRhythm ohhh thats super weird

Profile icon
Janatbek

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.

Profile icon
RandyTran5

@Janatbek Thanks! This helped :)

Profile icon
RohilPatel

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.

Profile icon
TheIslandRhythm

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

Profile icon
RohilPatel

Ur env file should look like this:

TOKEN=474mrifirm48

No spaces, nothing
@TheIslandRhythm

Profile icon
TheIslandRhythm

@RohilPatel it does

Profile icon
HahaYes

it looks like it works?

Profile icon
TheIslandRhythm

@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

Profile icon
HahaYes

@TheIslandRhythm huh yeah I don't know what happened

Profile icon
HahaYes

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

Profile icon
HahaYes

ah... did you import this from github?

Profile icon
TheIslandRhythm

@HahaYes I did actually

Profile icon
HahaYes

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

Profile icon
TheIslandRhythm

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

Profile icon
HahaYes