How to DM everyone with a bot - discord.py

Jose Miguel Herrera picture Jose Miguel Herrera · Aug 20, 2017 · Viewed 19.7k times · Source

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)

Answer

randomdude999 picture randomdude999 · Aug 26, 2017

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)