Treat a bot token as a password for the application. Store it in the service configuration or another access-controlled secret store, and have the code read it at startup. An application ID and a bot token serve different purposes; an ID is not a replacement credential.
Separate code from credentials
- Choose a clear variable name such as DISCORD_TOKEN and use that same name in your application.
- Set the value through the hosting service configuration available to your account.
- Keep local secret files out of version control and deployment archives.
- Limit panel access to people who need to operate the bot.
- Check that startup logs never print environment variables or authorization headers.
A local .env file is only a file until your runtime or application loads it. Adding one does not guarantee that your bot reads its values. Conversely, access to files or environment configuration can reveal secrets, so hiding a value in a settings screen does not replace account permissions.
Replace an exposed token
If a token appears in a public repository, screenshot or shared log, reset it in the Discord Developer Portal. Removing the visible copy alone does not revoke it. Stop affected deployments, update their configured secret, and start only the intended production instance.
Also remove the exposure from current files and review repository history, build artifacts and logs. Check other credentials stored beside it, such as database passwords. Rotate those when they were exposed too. Do not paste either the old or new token into a support ticket.
Confirm the replacement worked
Check the application identity in the startup log without logging its token, then run a harmless command in a test channel. Review unexpected bot actions and team access. If authentication still fails, check the variable name and deployment configuration before repeatedly generating new credentials.
Use separate credentials for a staging bot and review panel access after team changes.
Sources and references
Hosting documentation. Publication and update dates reflect this edition.
Related articles
- Updating a Discord Bot with a Separate Test DeploymentTest dependencies, commands and database changes without running competing copies of your production bot.2 min read
- Giving someone else access to your serverAdd a sub-user with limited permissions instead of sharing your password.1 min read
- Enabling two-factor authenticationAdd an authenticator app and store your recovery codes properly.1 min read
