DM Everyone Command

1.2k views Asked by At

I don't get why I have an error in this code:

@client.command()
async def dmall(ctx, message):
    for m in client.get_all_members():
        await m.send(message)
    await ctx.send("Done!")

My DMs are on, but here is the error: I have also tried using it with my alt, but same error.

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 49, in dmall
    await m.send("Hello! This is a DM :)")
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 850, in send
    channel = await self._get_channel()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 243, in _get_channel
    ch = await self.create_dm()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/user.py", line 715, in create_dm
    data = await state.http.start_private_message(self.id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 245, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50007): Cannot send messages to this user

Also, a side note, I am not using this for malicious purposes. I am aware that abusing this could lead to a ban.

2

There are 2 answers

0
Mike On

Using a try statement will let it skips all the users it can't message:

@client.command()
async def dmall(ctx, message):
    for m in client.get_all_members():
        try:
           await m.send(message)
        except:
           print("couldn't send message to "+m)
    await ctx.send("Done!")

it might be an error on discord's side, or maybe there's something wrong with your bot's permissions in the server.

0
Raphiel On

i actually have the code lying arround on my code folder for a while now, the code will be

@client.command()
async def dmall(ctx, *, message):
    guild = ctx.guild
    for m in guild.members:
        try:
            await m.send(message)
            await ctx.send(f"[Success] Successfully sent to {m}.")

        except Exception as e:
            await ctx.send(f"[Failure] Failed to send to {m}\n\n\n{e}")

    await ctx.send(f"[Done] Finnished!")

Note the await ctx.send(f"[Done] Finnished!") is not a typo.