Fix "An invalid token was provided" (TokenInvalid / LoginFailure)
Fix discord.js TokenInvalid and discord.py LoginFailure errors: get a fresh bot token, check your .env file and make sure the token actually loads.
Updated · 2 min read
"An invalid token was provided" means Discord rejected the token your bot used to log in, or your code passed no token at all. Reset the token in the Developer Portal, put the new one in your .env file, and make sure your code actually loads it.
What the error looks like
discord.js:
Error [TokenInvalid]: An invalid token was provided.
discord.py:
discord.errors.LoginFailure: Improper token has been passed.
Cause 1: the token is wrong or was reset
Tokens stop working when you click Reset Token, or when Discord resets it for you (for example, after detecting it in a public GitHub repository or after a crash loop exceeds the daily login limit). Fix it:
- Open the Discord Developer Portal and select your application.
- On the Bot page, click Reset Token and copy the new value.
- On Schokr, open the file manager, edit
.envand replace the old token. - Restart the server.
Cause 2: you copied the wrong value
Only the token from the Bot page works. These look similar but are not the token:
| Value | Where it's from | Used for |
|---|---|---|
| Bot token | Bot page | Logging in. This is the one you need. |
| Client secret | OAuth2 page | Web OAuth2 logins, not bots |
| Application ID / Public key | General information page | Invite links, slash commands, interaction verification |
Cause 3: the token isn't loaded (undefined)
If your code reads the token from the environment but .env isn't loaded, the library receives undefined or an empty string. Check:
.envis in the right folder. It must be in the folder the bot runs from, which on Schokr is the server root, next topackage.jsonor your main.pyfile.- The file is named exactly
.env, not.env.txtorenv. - You load it before using it. In Node.js,
require('dotenv').config();must be the first line. In Python, callload_dotenv()beforeos.environ[...]. - The variable name matches.
DISCORD_TOKENin the file andprocess.env.DISCORD_TOKENin code, same spelling and case. dotenvorpython-dotenvis in your dependencies, so it's installed on the server.
A quick check, without printing the token itself:
console.log('Token loaded:', Boolean(process.env.DISCORD_TOKEN), 'length:', process.env.DISCORD_TOKEN?.length);
print("Token loaded:", bool(os.getenv("DISCORD_TOKEN")), flush=True)
Cause 4: stray characters
- Quotes copied into the value are usually fine with dotenv, but spaces or line breaks inside the token aren't.
- Don't prefix it with
Bot. The libraries add that themselves. - Re-paste the token on a single line with nothing after it.
Still failing?
Reset the token once more, paste it carefully, and restart. If you still get the error, ask in the Schokr Discord, but never post the token itself.