Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Moderation and Security

Token and Secret Safety

Protect Discord and Telegram bot tokens: keep them out of Git, load from the environment, redact logs, scan for leaks, rotate after exposure, and use least privilege.

A bot token is a password. Whoever holds it can act as your bot: read what it can read, send messages in its name, kick members, and on Telegram take over the bot completely. Bots are leaked all the time, mostly by accident: a token pasted into a public repository, a screenshot of a terminal, a log line shared in a help channel. Automated scanners find tokens on public GitHub within minutes, and both Discord and Telegram scan for them, so a leaked Discord token is often reset for you within moments.

This lesson is about habits and small tools that make leaks unlikely and cheap to recover from. Every token in the examples is fake and generated on the spot.

What counts as a secret

Secret What it grants
Telegram bot token (123456789:AA...) full control of the bot
Discord bot token (three dot-separated parts) full control of the bot
Discord client secret, OAuth tokens act on behalf of users
Discord public key not secret (it only verifies signatures)
Webhook secret token (Telegram) lets anyone forge updates to your endpoint
Discord webhook URLs anyone with the URL can post in that channel
Database passwords, API keys of other services whatever they protect

Rule 1: keep secrets out of source code

Never write a token in a file that is committed. Put it in the environment and read process.env. Locally, keep a .env file that is listed in .gitignore, and commit a .env.example with fake values so teammates know what to set.

TEXT
# .gitignore
.env
.env.*
!.env.example
data/
*.db

Node 24 can load such a file itself with node --env-file=.env src/index.js, so no package is needed. In production, set the variables in your host's secret store, systemd EnvironmentFile, or container secrets, not in the image.

If a token ever lands in a commit, deleting it in a later commit is not enough: it remains in history. Treat it as leaked and rotate it (below).

Rule 2: never print them

Logs travel: to files, dashboards, error trackers and chat. A common leak is logging a whole request or config object. Add a redactor to your logger so tokens are masked even when someone logs carelessly.

Node.js Redact tokens from log lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Pattern-based redaction is a safety net, not a plan. The real protection is not logging secrets at all. Prefer logging fields you choose ({ chatId, command }) rather than whole objects, and never log request headers, because the Telegram secret header and Authorization headers live there.

Rule 3: scan before you push

A small script that looks for token-shaped strings in the files you are about to commit catches mistakes before the internet does. Wire it into a git pre-commit hook or your CI.

Node.js A tiny secret scanner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that the 64-character public key in the README is not flagged: it is not a secret. Real tools such as gitleaks and GitHub secret scanning do this with many more rules, and you should enable them on any public repository.

Rule 4: least privilege

If a token leaks, the damage is limited by what the bot is allowed to do.

  • Discord permissions: invite the bot with only the permissions it needs. A music bot does not need Administrator. Use the OAuth2 URL generator, check each box on purpose, and prefer roles over blanket permissions.
  • Discord intents: request only the gateway intents you use. MESSAGE_CONTENT, GUILD_MEMBERS and GUILD_PRESENCES are privileged and need to be enabled in the Developer Portal; do not enable them "just in case".
  • Telegram: set allowed_updates, keep privacy mode on in groups (the bot then sees only commands and replies to it), and limit who can add the bot with BotFather's /setjoingroups.
  • Slash commands: use default_member_permissions so sensitive commands are hidden from ordinary members, and still check permissions in your code (see moderation commands and permissions).

Rule 5: know how to rotate

When a secret leaks, act in this order:

  1. Revoke it at once. Discord: Developer Portal, Bot, Reset Token. Telegram: BotFather, /revoke, choose the bot. The old token stops working immediately.
  2. Put the new token in your secret store and redeploy.
  3. Check what happened: audit logs, unexpected messages, new webhooks, changed settings.
  4. Remove the secret from history if it was committed (a force-push and a fresh clone for everyone), but assume it is burned regardless.

Practice the steps once before you need them, and keep a note of where each secret is configured, so rotation takes minutes.

Rule 6: protect the things you accept

Secrets also flow inward. Verify Telegram's secret header and Discord's signatures (Telegram webhook server, Discord interactions). Treat user text as data: do not pass it to eval, shell commands or string-built SQL. If your bot can run user-supplied commands or code, run them in a sandbox without access to your environment variables.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You committed a token and removed it in the next commit. What now?

    The token still exists in Git history and may already have been scraped.
  2. 2. Which of these is NOT a secret?

    The public key only verifies signatures and is shown openly in the Developer Portal.
  3. 3. What is the best protection against secrets in logs?

    Choose the fields you log. Redaction catches mistakes but should not be the only control.
  4. 4. Why does least privilege matter for leaked tokens?

    A token for a bot without Administrator or privileged intents can do far less damage.

Summary

  • Tokens and secrets live in the environment or a secret store, never in committed code; .env goes in .gitignore.
  • Log chosen fields, redact token patterns, and scan commits for leaks.
  • Invite bots with minimal permissions and intents.
  • When a secret leaks, revoke first, then redeploy, then investigate.
  • Verify everything inbound and treat user input as data.

Next: Rate limits and backoff