Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DM everyone with a bot - discord.py

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)
like image 705
Jose Miguel Herrera Avatar asked Aug 20 '17 18:08

Jose Miguel Herrera


People also ask

How do you DM all members on a Discord server?

guild. members for member in members: try: await member. send(args) print("'" + args + "' sent to: " + member.name) except: print("Couldn't send '" + args + "' to " + member.name) else: await ctx.channel. send("You didn't provide a arguments.")

Can Discord bots DM users?

Bots need to have at least 1 common server with a user to be able to send a direct message. If the user is on the same server as the bot, only then you can send a DM, using any of the other methods on this post.

Does discord.py support slash commands?

discord.py does not support slash commands and will never add support for slash commands (as it has shut down) thus I recommend disnake (a popular fork).


1 Answers

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)
like image 105
randomdude999 Avatar answered Nov 10 '22 21:11

randomdude999