Free game servers for Minecraft, Terraria, CS & Discord · No credit card required!
Discord Bots6 min•November 9, 2026

How to Prevent Discord Bot Downtime: Crash Recovery, Auto-Restart & Memory Leaks

Fix common Discord bot crashes: unhandled promise rejections, WebSocket gateway timeouts, memory leak profiling, PM2 ecosystem setups, and automated sub-second cloud restarts.

CoalByte SRE Team
CoalByte SRE Team
Reliability Engineer
How to Prevent Discord Bot Downtime: Crash Recovery, Auto-Restart & Memory Leaks

#Why Do Discord Bots Crash? The Top 3 Culprits

Even experienced developers encounter sudden bot offline incidents. The three most prevalent reasons include:

  1. Unhandled Exceptions: A Discord user deletes a message before your bot fetches it, causing an uncaught DiscordAPIError[10008]: Unknown Message that exits the Node.js process with exit code 1.
  2. WebSocket Gateway Resets: Discord periodically cycles gateway shards. If your client fails to resume session tokens properly, the connection hangs indefinitely.
  3. Memory Leaks: Caching every guild member, reaction, and message in memory eventually breaches the 1 GB V8 heap limit.

1. Robust Global Error Handlers

In Node.js, ensure you capture unhandled rejections without letting the Node runtime terminate immediately:

javascript
process.on('unhandledRejection', (reason, promise) => {
  console.error('⚠️ [Anti-Crash] Unhandled Promise Rejection:', reason);
  // Log to external telemetry or alert channel
});

process.on('uncaughtException', (error, origin) => {
  console.error('🔥 [Anti-Crash] Uncaught Exception:', error, 'Origin:', origin);
  // Gracefully attempt reconnect or clean exit for supervisor restart
});

process.on('uncaughtExceptionMonitor', (error, origin) => {
  console.error('🔍 [Anti-Crash] Exception Monitor:', error);
});

2. PM2 Ecosystem Configuration (`ecosystem.config.cjs`)

In production environments, a process supervisor like PM2 ensures that if an unavoidable crash occurs, your bot is instantly resurrected:

javascript
module.exports = {
  apps: [
    {
      name: 'discord-bot-prod',
      script: 'index.js',
      instances: 1,
      autorestart: true,
      watch: false,
      max_memory_restart: '1500M',
      exp_backoff_restart_delay: 100,
      env: {
        NODE_ENV: 'production'
      }
    }
  ]
};

Start and monitor with:

bash
pm2 start ecosystem.config.cjs
pm2 status
pm2 logs --lines 100

3. Memory Optimization: Tuning Discord.js Caches

Stop caching messages and presences you don't use:

javascript
import { Client, Options } from 'discord.js';

const client = new Client({
  makeCache: Options.cacheWithLimits({
    MessageManager: 50,       // Keep only last 50 messages per channel
    PresenceManager: 0,       // Do not cache user presences (huge RAM saver)
    ReactionManager: 0,
    UserManager: 1000,
    GuildMemberManager: 500
  }),
  intents: [...]
});

4. Zero-Maintenance Restarts on CoalByte Cloud

On CoalByte Free Discord Bot Hosting, every container is continuously supervised. If your script exits for any reason, the cloud hypervisor restarts your process in under 1.5 seconds, ensuring 99.9% uptime for your community.

Tags:#Crash Recovery#Discord Bots#PM2#Docker#Node.js#Python#24/7 Uptime
⚡ 100% Free · No Credit Card Required

Ready for 24/7 high-performance free hosting?

Launch your dedicated server with 3 GB RAM, 2 vCPU, 8 GB fast SSD, and Anycast Anti-DDoS in under 60 seconds.

Deploy Free Server
All Articles