Why does my Discord bot stop responding after some time?

I have created a Discord bot that functions as intended initially, but it becomes unresponsive after running for a while. While the bot appears online, I cannot execute any commands, including the shutdown command.

I’m uncertain whether the issue lies within the code or if it’s related to the environment. I typically run the bot in Visual Studio Code, but encountered errors when running it via the command prompt due to a missing main function.

from discord.ext import commands
import discord
import asyncio

BOT_TOKEN = "your_bot_token_here"
CHANNEL_ID = 123456789

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())

@bot.event
async def on_message(message: discord.Message):
    if "https://twitter.com/" in message.content:
        new_message = message.content[:8] + "vx" + message.content[8:]
        await message.channel.send(new_message)
        await message.delete()
    await bot.process_commands(message)

@bot.event
async def on_message(message: discord.Message):
    if "https://x.com/" in message.content:
        new_message = message.content[:8] + "fixv" + message.content[8:]
        await message.channel.send(new_message)
        await message.delete()
    await bot.process_commands(message)

@bot.event
async def on_message(message: discord.Message):
    if "https://www.tiktok.com/t/ZT8QnN8hg/" in message.content:
        new_message = message.content[:8] + "vx" + message.content[8:]
        await message.channel.send(new_message)
        await message.delete()
    await bot.process_commands(message)

@bot.command()
@commands.is_owner()
async def shutdown(context):
    exit()

bot.run(BOT_TOKEN)

As I’m still learning Python, I’m curious about potential reasons for this freezing issue. Could it result from my computer entering sleep mode? Or could there possibly be a memory leak? Any guidance would be greatly appreciated because I have not found satisfactory information online regarding this problem.

Your bot’s freezing because Discord’s gateway connection drops and doesn’t reconnect properly. This happens all the time when bots run for hours without proper connection handling. Your computer going to sleep will definitely cause this - it kills the network connection. I had the exact same problem running my bot locally and switched to cloud hosting, which fixed the constant disconnects. For local development, you need proper connection error handling and should use something like PM2 to auto-restart your bot when it crashes. Also check if your internet’s stable - even unstable connections make Discord bots unresponsive while they still show online.

I’ve experienced similar issues with my Discord bots, and in my case, unhandled exceptions or network interruptions were the culprits. Your bot might be exceeding rate limits or encountering errors that go unlogged. Consider wrapping your message handling with try-except blocks to diagnose any failures. It’s also important to note that Discord’s API may disconnect bots periodically without adequate reconnection strategies. Implement logging to monitor when your bot becomes unresponsive, and check your hosting environment for potential timeout settings. Proper error handling and logging can provide insights into the underlying problems.

your issue is def the multiple on_message events - you can only have one and the others get overwritten. Combine them into a single function using if/elif statements instead. Also, your shutdown command should use await bot.close() not exit().