Search code examples
pythonpython-3.xdiscordpycord

Why is Discord API returning Error 403 when sending a DM with valid permissions?


I currently have a bot for one of my clients that can DM all users under a specific role. For my test server, the command works as intended with no issues. The only time Discord returns a 403 is when the member the bot was trying to DM has DM's disabled, which is fine. However, when I pushed the changes to my client's branch, his server does not work at all and instead returns 403 errors for every DM the bot tries to send. The bot has admin perms on his server, and the bot's role is higher than every other role except the server owners. I should also mention I am using the latest version of the PyCord wrapper, as well as Python 3.11.

I have tried checking for proper intents, making sure all the attempted DM's had DM's open, as well as overviewing the client's source code compared to mine. This works exactly as intended in my test server, just not his. The DM loop loops through every member, but simply returns the 403 - Invalid Permissions error in the log of the bot.

Here is the snippet of code that attempts to send a DM to every member with a specific role:

# send dm and create
    async def send_dms(image_url):
        # if image is passed in arg
        if image_url is not None:
            embed.set_image(url=image_url)

        # create dm role list
        roles = [role, role2, role3]

        # set description
        embed.description = message

        failures = 0
        successes = 0
        for member in ctx.guild.members:
            if any(item in roles for item in member.roles):
                try:
                    await member.send(embed=embed)
                    successes += 1
                except:
                    failures += 1

        if image is True:
            await confirm(ctx, f"`{successes}` successful DM's sent with `{failures}` failures.", eph=True)
        else:
            await edit_confirm(ctx, f"`{successes}` successful DM's sent with `{failures}` failures.")

Please leave a comment if you have any questions and I will promptly update the question.


Solution

  • After weeks of searching, we finally found the answer. The bot was "quarantined" by Discord for sending too many DM's in a short period of time, which was flagged as "suspicious".

    There is really no solution to this aside from creating a new bot or contacting Discord in hopes they unban you.