A bot's token is its password: whoever gets it can write, read and operate in its name. The most common incident is not an attack but an accident: the token ends up in source code, a screenshot or a request for help.
This guide follows the official Discord and Telegram documentation and keeps secrets in the run environment, not in code.
1. What counts as a secret?
| Secret | Where does it come from? | What to watch |
|---|---|---|
| Discord bot token | The Developer Portal Bot page | According to the documentation you can only see it again by regenerating it; never share it or commit it. |
| Telegram bot token | BotFather | For a lost or leaked token BotFather's /token command issues a new one. |
| Webhook secret_token | You invent it when calling setWebhook | A long random value; the endpoint should verify it. |
| API keys, database password | External services and the server | A separate key per service, with narrow permissions. |
2. An environment variable instead of code
- Create a .env file on your development machine and add it to .gitignore before you make the first commit.
- Put a .env.example file in the repository with placeholder values only, so others can see which variables are needed.
- In production enter the variables in the hosting panel; do not upload the .env file to the server or the repository.
- Read them in your program with process.env (Node.js) or os.environ (Python), and stop at startup with a clear error message if one is missing.
DISCORD_TOKEN=ide-kerul-a-token
TELEGRAM_BOT_TOKEN=ide-kerul-a-token
WEBHOOK_SECRET=hosszu-veletlen-ertek3. If the token has leaked
- On Discord open the Developer Portal Bot page and choose Reset Token; the old token becomes invalid.
- On Telegram request a new token with BotFather's /token command.
- Replace the value in the hosting panel and restart the bot.
- Review the log and the channel activity: was there any unauthorised messaging or settings change?
- If the token went into version control the old value is no longer usable, so cleaning history is secondary; the point is the replacement. Rotate the other secrets too if they leaked together.
4. Narrow permissions and asking for help safely
- Enable only the privileged intents the bot really needs; according to the documentation they give access to sensitive data such as message content and may require approval.
- When asking for help do not send a token, a full .env file or a screenshot that shows one; remove secrets from logs before sharing.
- The webhook endpoint should accept only requests authenticated by Telegram's secret_token header.
- Rotate the token from time to time and give hosting panel access only to those who need it.



