Discord.py bot fail to send message after certian amount

Solution 1:

Your bot is being rate limited.

Discord will prevent bots and users from sending too much data to their servers. (presumably to save bandwidth)

If you want to fix your problem add a delay between messages, I recommend a 1sec delay between messages.

Here's some code that will do that:

import time

@bot.command()
async def dm_all(ctx, *, args=None):
    if ctx.author.id != my_id:
        return
    counter, Gcounter, Fcounter = 0,0,0 
    if args is not None:
        members = ctx.guild.members
        for m in members:
          if(m.bot):
            continue
          counter=counter+1 
          await ctx.send('trying to send message to: ' + str(m.display_name))
          time.sleep(1)
          try:
            await m.send(args)
            await ctx.send("Message was send")
            Gcounter=Gcounter+1
          except:
              await ctx.send('didnt work')
              Fcounter=Fcounter+1
        await ctx.send("Send messaged to: " + str(Gcounter) +
        "\n failed to send to: " + str(Fcounter) + 
        "\n success rate is: " + str((Gcounter/counter)*100) + "%")
    else:
        await ctx.send('please provide a message')

It will take a while to send the message to all users, but there's nothing you can do about that.