4 KiB
4 KiB
Discord Bot Token Invalid - Troubleshooting & Fix
Problem
The Discord bot is failing to login with error:
❌ Unhandled Promise Rejection: Error [TokenInvalid]: An invalid token was provided.
This means the DISCORD_BOT_TOKEN environment variable is either:
- Expired or revoked
- Invalid/malformed
- For a bot that was deleted from Discord Developer Portal
- Not properly synced to the deployment environment
Solution: Get a New Bot Token
Step 1: Go to Discord Developer Portal
- Visit: https://discord.com/developers/applications
- Sign in with your Discord account
- Click on the application named "AeThex" (or your app name)
Step 2: Get the Bot Token
- Click on the "Bot" tab on the left sidebar
- Under the "TOKEN" section, click "Reset Token"
- Click "Yes, do it!" to confirm (the old token will be revoked)
- Click "Copy" to copy the new token
Step 3: Update the Environment Variable
The new token looks like: NTc4OTcxMjQ1NDU0OTUwNDIx.GxxxXX.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
For Railway/Spaceship Deployment:
- Go to your Railway/Spaceship dashboard
- Go to environment variables
- Update or add:
DISCORD_BOT_TOKEN=<new_token_here> - Save changes
- Redeploy the bot
For Local Development:
- Update
.envfile incode/discord-bot/:DISCORD_BOT_TOKEN=your_new_token_here - Restart the bot:
npm start
Step 4: Verify the Bot
After updating the token, the bot should:
- Login successfully:
✅ Bot logged in as AeThex#xxxx - Load all commands:
<EFBFBD><EFBFBD><EFBFBD> Loaded command: verify, etc. - Start health check server:
🏥 Health check server running on port 8044
Token Format Validation
A valid Discord bot token:
- ✅ Starts with a number (user ID)
- ✅ Contains 3 parts separated by dots:
part1.part2.part3 - ✅ Is ~70+ characters long
- ✅ Contains only alphanumeric characters, dots, hyphens, and underscores
Example: NTc4OTcxMjQ1NDU0OTUwNDIx.GxxxXX.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Common Issues & Fixes
Issue: "Token has been exposed"
Discord automatically revokes tokens if they appear in public repositories (GitHub, etc.).
Fix: Reset the token in Discord Developer Portal as described above.
Issue: Bot not in any servers
Even with a valid token, the bot might not connect to your servers.
Fix:
- In Discord Developer Portal, go to OAuth2 > URL Generator
- Select scopes:
bot - Select permissions:
Administrator(or specific permissions) - Copy the generated URL and open it in browser
- Select your server and authorize
Issue: "TokenInvalid" but token looks correct
The token might be:
- Copied with extra whitespace
- Not fully copied (missing characters)
- From a different bot application
Fix:
- Go back to Discord Developer Portal
- Reset the token again (this revokes the old one)
- Copy the full new token carefully (no extra spaces)
- Update environment variable immediately
Environment Variables Checklist
Before deploying, verify these are set:
- ✅
DISCORD_BOT_TOKEN- Valid bot token from Developer Portal - ✅
DISCORD_CLIENT_ID- Usually578971245454950421 - ✅
DISCORD_PUBLIC_KEY- From General Information tab - ✅
SUPABASE_URL- Database connection - ✅
SUPABASE_SERVICE_ROLE- Database authentication - ✅
BOT_PORT- Usually3000or8044for Railway
Still Having Issues?
If the token is valid but bot still won't login:
- Check Discord Server Status: https://status.discord.com/
- Verify Bot Permissions: Make sure bot has "Send Messages", "Read Messages"
- Check Firewall: Some firewalls block Discord API connections
- Review Recent Changes: Did you recently update discord.js version?
- Clear Cache: Delete
node_modules/and runnpm installagain
References
- Discord Developer Portal: https://discord.com/developers/applications
- Discord.js Documentation: https://discord.js.org/
- Creating a Discord Bot: https://discordjs.guide/preparations/setting-up-a-bot-application.html