My Discord bot's /broadcast feature stops working after sending to about 40 servers

Hey everyone! I’m having a weird issue with my Discord bot. I made a /broadcast command that’s supposed to send a message to all servers the bot is in. Only I can use it as the bot owner.

I use it to share updates and error info. But here’s the problem: it stops working after sending to around 40 servers. The strange part is that it doesn’t show any errors. The bot keeps running, but /broadcast just stops.

I even added code to skip servers where the bot can’t write. Here’s a simplified version of what I’m working with:

@commands.is_owner()
@bot.command(name="broadcast")
async def send_to_all(ctx, message: str):
    sent_count = 0
    for guild in bot.guilds:
        for channel in guild.text_channels:
            if channel.name in allowed_channels and channel.permissions_for(guild.me).send_messages:
                try:
                    await channel.send(message)
                    sent_count += 1
                    print(f"Sent to {guild.name} - {channel.name}")
                except Exception as e:
                    print(f"Error in {guild.name}: {e}")
    print(f"Broadcast done. Sent to {sent_count} channels.")

Any ideas why it might be stopping early? Thanks for your help!

hey there! sounds like you’re hitting a rate limit. discord restricts how many messages u can send in a short time. try adding a small delay (like 1-2 seconds) between each message send. that should fix it :slight_smile: good luck with ur bot!

I’ve run into this exact problem before with my own Discord bot. The culprit is definitely Discord’s rate limiting. What worked for me was implementing a cooldown system using asyncio.sleep(). Here’s a quick example of how you could modify your code:

import asyncio

@commands.is_owner()
@bot.command(name="broadcast")
async def send_to_all(ctx, message: str):
    sent_count = 0
    for guild in bot.guilds:
        for channel in guild.text_channels:
            if channel.name in allowed_channels and channel.permissions_for(guild.me).send_messages:
                try:
                    await channel.send(message)
                    sent_count += 1
                    print(f"Sent to {guild.name} - {channel.name}")
                    await asyncio.sleep(1.5)  # Add a 1.5 second delay between messages
                except Exception as e:
                    print(f"Error in {guild.name}: {e}")
    print(f"Broadcast done. Sent to {sent_count} channels.")

This should allow your bot to send messages to all servers without hitting the rate limit. You might need to adjust the sleep time depending on your specific needs and Discord’s current rate limit policies. Hope this helps!

I encountered a similar issue with my bot’s broadcasting feature. The problem likely stems from Discord’s rate limiting mechanisms. To resolve this, I recommend implementing a backoff strategy. Start by adding a short delay between messages, then gradually increase it if you encounter failures. Additionally, consider batching your broadcast messages or spreading them out over a longer period. This approach helped my bot successfully broadcast to hundreds of servers without hiccups. Remember to monitor your bot’s performance and adjust the delay as needed to find the optimal balance between speed and reliability.