How to DM everyone with a bot - discord.py
Asked Answered
M

1

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)
Murat answered 20/8, 2017 at 18:49 Comment(1)
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...Gattis
U
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)
Unthoughtof answered 26/8, 2017 at 10:8 Comment(2)
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!Murat
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.Uvarovite

© 2022 - 2024 — McMap. All rights reserved.