An invalid token was provided
What you see
Error [TokenInvalid]: An invalid token was provided.
Why it happens
discord.js throws this in two cases. login() got no token at all, often because process.env.TOKEN is empty when the .env file isn’t loaded. Or Discord rejected the token: the placeholder from ChatGPT’s code is still there, something other than the bot token was pasted, or the token was reset.
The fix
- rightsocket does this Moves a token written in the code into a .env file, and makes npm start load that file (node --env-file=.env) in place of the dotenv package ChatGPT’s code often needs but doesn’t install.
- rightsocket does this Lists every process.env name the code reads in .env.example, so the one login() uses isn’t left empty.
- You do this Get the token from the Discord Developer Portal: your app, then Bot, then Reset Token. In .env, paste it after DISCORD_TOKEN= (or the name your code reads, such as TOKEN=) with no quotes or spaces.
Sources
- discord.js source: WebSocketManager turns Discord’s 401 into TokenInvalid (v14)
- discord.js source: error messages
- Stack Overflow: "Error [TokenInvalid]: An invalid token was provided"