Keep your bot running: auto-restart and logging basics
Bots crash for many reasons, such as a network hiccup, an unhandled error or running out of memory. You cannot prevent every crash, but you can make your bot recover and tell you why it failed.
Read the Console first
The Console tab shows everything your bot prints, including error messages. When a bot stops, scroll up to the first error. That line usually names the file, the line number and the cause.
Handle errors in code
- Wrap risky actions, like API calls and database queries, in try and catch (or try and except) blocks.
- Log the error instead of letting the whole bot exit.
- Use your library's global error handler for commands, so one bad command does not stop everything.
Log to a file
Printing to the Console is useful, but a log file keeps history. Python has the built-in logging module, and Node.js has several logging packages. Write messages with a time and a level such as info, warning or error. You can then open the log file in the Files tab. Rotate or delete old logs so they do not fill your storage.
Auto-restart
Many panels have an option that restarts a server after it stops unexpectedly, but features vary. Check your panel settings or ask us in our Discord whether auto-restart is available for your bot. Do not assume it is on by default.
A simple restart-safe checklist
- Make sure your bot starts cleanly from a fresh start with one command.
- Do not depend on data stored only in memory. Save important data to a file or database.
- Reconnect logic: most libraries reconnect to Discord or Telegram automatically, so avoid overriding that.
- Watch memory use. A steadily growing number can mean a memory leak.
Common causes of repeated crashes
- Invalid or reset token
- Missing package after an update
- Too little RAM for what the bot does
- An error in a loop that runs on startup
If your bot restarts over and over, stop it, fix the first error you see, and then start it again. A restart loop hides the real problem.