6

Well, I'm doing a Direct Message function to DM a specific user, but I've been searching the way to do it so I can message everyone on a server, and I don't get it. I'm using discord.py 0.16.9 for reference. Here is my current code:

@client.command(pass_context = True)
async def dm(ctx, member : discord.Member = None, *, message):
    if not ctx.message.author.server_permissions.administrator:
        return
    if not member:
        return await client.say(ctx.message.author.mention + "Specify a user to DM!")
    if member = "@everyone":
        member = 
    else:
        await client.send_message(member, message)
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
  • Why are you wanting to mass DM everyone on the server? Wouldn't that be a concern on the ratelimits of your bot? Is this not something that can be fixed with a everyone ping in the server? Reference: You can only send 120 messages every 60 seconds... – Sam Rockett Aug 21 '17 at 12:22

1 Answers1

10

As already stated in a comment, it really isn't a good idea to dm everyone, but if you must, you can iterate over all members of a server and message them individually. In your example:

if member == "@everyone":
    for server_member in ctx.message.server.members:
        await client.send_message(server_member, message)
randomdude999
  • 701
  • 7
  • 20
  • Gee, thanks, it's for a testing server of my bot, I also study the code to know what it does and this will help me ^^ THX! – Jose Miguel Herrera Aug 26 '17 at 20:57
  • 3
    There's no reason to wait each message to return a response before sending the next message, especially since `discord.py` handles rate-limiting quietly and natively; it would be better to use `asyncio.gather` or `asyncio.wait`. – Sebastian Mendez Apr 25 '18 at 22:25