Боты Discord2 минуты на чтение

Fix Discord TokenInvalid and discord.py LoginFailure

Fix an invalid Discord bot token by checking what your program reads, updating the deployment secret and verifying a successful login.

Check whether the running process has the token

TokenInvalid in discord.js and LoginFailure in discord.py can come from a missing environment variable or a token Discord has rejected. Compare the exact variable name in the login call with the name in the deployment’s Variables. Bothosting’s starters use BOT_TOKEN.

Temporarily log only whether the value is present. Start the process through the deployment dashboard so the check runs with the same environment as the bot. Remove the check when you finish.

Check whether the token is present in Node.js
console.log("BOT_TOKEN is set", Boolean(process.env.BOT_TOKEN?.trim()));
Check whether the token is present in Python
import os
print("BOT_TOKEN is set", bool(os.getenv("BOT_TOKEN", "").strip()))

Update the deployment with the current bot token

  1. Open the Discord Developer Portal and select the application installed in your test server. In Bot, confirm you’re using its bot token. The application ID, public key and OAuth client secret serve other purposes.
  2. If the token was reset, exposed or lost, reset it for that application. Last time’s token becomes invalid. Update any other authorized copy that needs the new value.
  3. Задано BOT_TOKEN in Variables as the raw token. Remove surrounding quotes, spaces and any Bot prefix. Save and restart. Keep the token out of the repository and Console.

See Discord’s app setup и deployment secrets guide.

Verify login and a Discord reply

Look for the ready event in Console and send /ping to the test app. If login succeeds but registration fails, check the slash command guide. If the gateway closes with 4014, the next step is to fix the requested intents.