A chat bot is not a website but a continuously running program. A Discord bot keeps a persistent connection to Discord, and a Telegram bot either keeps polling for new messages or waits for them at a public address. If the program stops, the bot becomes unreachable.
Based on the official Discord and Telegram documentation, this guide shows when a connection normally drops, when automatic restart is enough, and when restarting can do more harm than good.
1. How does a Discord bot keep its connection?
The Discord gateway announces heartbeat_interval in the Hello message, and the bot must send a heartbeat at that interval; Discord replies to each with a Heartbeat ACK. If no ACK arrives before the next heartbeat, the documentation treats the connection as zombied: close it, reconnect and attempt to Resume.
| Event | What must the bot do? |
|---|---|
| Reconnect (op 7) or a dropped connection | Reconnects to the resume_gateway_url from the Ready event and sends Resume; it does not identify again. |
| Invalid session (op 9), d: true | Resumes the session the same way. |
| Invalid session (op 9), d: false | Closes the connection, opens a new one and sends a fresh Identify. |
| Missing Heartbeat ACK | Closes the connection with any code other than 1000 or 1001, then reconnects. |
2. Why does it not work on every kind of hosting?
A program that holds a persistent connection must run continuously. Environments that stop or scale a process to zero when idle are not suitable for the Discord gateway. According to grammY's documentation, a Telegram webhook suits serverless or scale-to-zero environments, while long polling suits an always-on server.
| Run mode | Discord gateway bot | Telegram long polling | Telegram webhook |
|---|---|---|---|
| Always-on process | Suitable | Suitable | Suitable |
| Process that stops when idle | Not suitable: the connection drops | Not suitable: the bot stops polling | Possible if a request starts it |
3. What does automatic restart solve, and what does it not?
A restart loop is dangerous on Discord: according to the documentation an application may send at most 1,000 Identify calls per 24 hours, and exceeding it terminates all sessions and resets the bot token. Resume calls do not count. Limit restarts and allow quick repeats only after the fault is fixed.
| Problem | Does a restart help? |
|---|---|
| Crash from an unhandled exception | Yes if the cause is temporary; the bug must still be fixed from the log. |
| Out of memory | Only temporarily; the memory leak or the small allocation must be addressed. |
| Wrong token or missing setting | No; it causes a repeating crash loop. |
| Code that fails on every message | No; the bug returns after the restart. |
4. Operations checklist
- Run the bot as an always-on process and enable automatic restart with a delay.
- Keep the log and check what caused the last crash.
- Watch memory use; if it stays near the limit, move to a larger plan or look for a leak.
- Store the token and settings in environment variables so a missing setting does not cause a loop.
- For a larger community plan Discord sharding: according to the documentation it is required at 2,500 or more guilds.



