discord.py bot keeps disconnecting: 6 fixes
Why a discord.py bot disconnects, reconnects or shows "heartbeat blocked" warnings, with code fixes for blocking calls, rate limits, memory and crashes.
Seeing your discord.py bot drop offline, reconnect, or print warnings like Shard ID None heartbeat blocked? These are the usual causes, from most to least common.
1. Blocking code freezes the heartbeat #
discord.py runs on asyncio. If one command blocks (for example time.sleep(), requests.get(), heavy file work or a long loop), the bot can't send its heartbeat to Discord and the connection drops.
# Bad: blocks the whole bot
time.sleep(5)
r = requests.get(url)
# Good
await asyncio.sleep(5)
async with aiohttp.ClientSession() as s:
async with s.get(url) as r:
data = await r.json()
# Heavy work that has no async version
result = await asyncio.to_thread(slow_function, arg)
Warnings like "Shard ID None heartbeat blocked for more than 10 seconds" almost always mean this.
2. Rate limits (429) or a temporary block #
Sending too many requests (spamming messages, editing in a loop, restarting the bot over and over) gets you rate limited. On shared hosting IPs you can also be hit by other people's traffic. Slow down loops, don't log in again on every error, and let discord.py's built-in reconnect do its job.
3. Running out of memory #
If the process uses more RAM than it has, it gets killed and restarts. Check the RAM graph in your panel. Big caches (members, messages) are the usual cause: turn off intents and caches you don't need, or move to a bigger plan. See How much RAM does a Discord bot need.
4. Unhandled errors and crashes #
An exception in on_ready, a background task or a startup script can stop the bot. Add an error handler and wrap background loops:
@bot.event
async def on_command_error(ctx, error):
print(f"Command error: {error!r}")
@tasks.loop(minutes=5)
async def job():
try:
await do_work()
except Exception as e:
print(f"job failed: {e!r}")
5. Token, intents or two copies running #
- Token reset: if you (or Discord, after a leak) reset the token, the old one stops working. Update it in your startup variables.
- Privileged intents: if you request Members or Message Content without enabling them in the Developer Portal, the bot fails to connect. See Discord bot intents.
- Two copies: a copy on your PC and one on a host fight over the same session. Run only one.
6. The machine itself goes offline #
If the bot runs on your PC, sleep and Wi-Fi drops disconnect it. Moving it to a host fixes that: see Discord bot goes offline when I close my PC. ESN runs Python bots on Python 3.12 from $0.50/month, with a console that shows the last error before a crash.
/botfix command in the ESN Systems Discord. See bot plansStill stuck? Use the live chat on the panel or ask in the ESN Systems Discord. Last updated November 3, 2026.