ACLClouds
ACLClouds

The bot refuses to connect: token missing or invalid

Check the variable name, that the secret is set and the Discord permissions, without exposing it in the logs. Paste only the token value.

A token can be valid yet never read if the code expects TOKEN while the platform saves it as DISCORD_TOKEN. It must stay in an environment variable, never in the repository.

Step-by-step procedure

  1. Check the exact name

    Locate process.env.DISCORD_TOKEN or os.getenv("DISCORD_TOKEN") then use the exact same key.

  2. Remove spaces and quotes

    Paste only the token value. Do not put quotes around it in the “Discord bot token” field.

  3. Reset it if exposed

    If the token appears in a log, a screenshot or Git, reset it immediately in the Discord Developer Portal.

  4. Check the privileged intents

    An error related to privileged intents is fixed in the Discord Developer Portal and in your code, not by changing the token.

Check the variable without printing the token

const token = process.env.DISCORD_TOKEN;
console.log('DISCORD_TOKEN present:', Boolean(token), 'length:', token ? token.length : 0);

client.login(token);

Where to save the token

In the “Startup” tab, the “Discord bot token” card saves the token under the name DISCORD_TOKEN. Spaces and line breaks pasted by mistake around the value are removed on save. The token is stored encrypted and never shown again: to change it, enter a new one.

If your code reads another name, for example TOKEN or BOT_TOKEN, you have two solutions: change the code to read DISCORD_TOKEN, or add a variable with that name in the “Environment variables” section of the same tab. A name only accepts uppercase letters, digits and _. Either way, a saved value only applies at the bot’s next start.

Missing token or refused token

After a stop, the console tells apart two situations that are not fixed the same way.

In both cases, the card has a field for the value and the “Save and restart” button, so you do not need to switch tabs.

  • “Variable DISCORD_TOKEN missing or invalid”: the code received no value. The name read by the code and the saved name do not match, or nothing has been saved yet.
  • “Discord token refused”: a value was passed, but Discord does not accept it. The token was regenerated, cut off when copied, or it is not a bot token.

Check connection

After the restart, the console should no longer show a login error and the bot should appear online in the member list of your Discord server. For a safe check, print whether the token is present and how long it is, never its value.

Frequently asked questions

What does Error [TokenInvalid]: An invalid token was provided. mean?

discord.js received a value that Discord rejects, or no value at all: client.login(undefined) produces the same error. First check that the name read from process.env is DISCORD_TOKEN. If it is, open the Discord Developer Portal, “Bot” tab, click “Reset Token”, copy the whole new token and save it.

And discord.errors.LoginFailure: Improper token has been passed.?

This is the discord.py equivalent. The most common cause is using the wrong value: the “Client Secret” from the “OAuth2” tab or the application ID are not bot tokens. The right one is copied from the “Bot” tab. If os.getenv returns None, the error becomes a TypeError: the variable name does not match.

I saved the right token and nothing changes, why?

Two causes come up often. The bot was not restarted, although a variable only applies at the next start. Or a .env file uploaded with the project holds an old token and your code loads it with the override option, which replaces the value provided by the platform. Delete that file from the server: it is not needed.

What should I do if “Discord access suspended for a while” appears?

The bot attempted too many connections to Discord in a short time, usually because it kept restarting with a refused token. Fix the token, then wait: access comes back by itself. Do not regenerate the token several times in a row or restart the bot in a loop, as this extends the suspension.