0

I am looking for a way to direct message everyone in a discord server using discord.py with a certain role with a custom message of my choice (Preselected) I have tried numerous different methods, none of which have worked. Please help me!

Logicsx
  • 1
  • 1
  • 2
  • 2
    What exactly have you tried? \ – Mick_ Apr 03 '18 at 22:28
  • Possible duplicate of [How to DM everyone with a bot - discord.py](https://stackoverflow.com/questions/45785727/how-to-dm-everyone-with-a-bot-discord-py) – Mick_ Apr 03 '18 at 22:34

1 Answers1

3

Iterate over all the Members on the Server, and check if they have the specified role.

from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def message_role(ctx, role: discord.Role, *, message):
    for member in ctx.message.server.members:
        if role in member.roles:
            await bot.send_message(member, message)

bot.run('TOKEN')

This would be invoked like

!message_role messageable Hi everybody!

That would send Hi everybody! to everyone with the messageable role.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • i recieve this error discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Att ributeError: 'Context' object has no attribute 'server' – Logicsx Apr 05 '18 at 20:12
  • Oops, that should be `ctx.message.server` I will edit my answer – Patrick Haugh Apr 05 '18 at 20:14