Reputation: 73
How would I send a private message to a specific role in Python
Here's what I've already done:
@bot.command(name='dm')
async def on_message(ctx):
role = get(ctx.guild.roles, id=839182749452992632)
for user in ctx.guild.members:
if user.roles == role:
await bot.send_message(user, 'hello')
Upvotes: 3
Views: 785
Reputation: 2289
user.roles
returns a list, however you're only comparing it to a single role
object. This will always be False
. Instead, check if the desired role is in the user.roles
. If so, check if there already is a DM with that user, otherwise create one and send the message.
import asyncio
@bot.command(name='dm')
async def on_message(ctx):
role = get(ctx.guild.roles, id=839182749452992632)
for user in ctx.guild.members:
if role in user.roles:
if user.dm_channel is None:
await user.create_dm()
await user.dm_channel.send("hello")
await asyncio.sleep(1)
You should however also include a delay, to not get flagged by discord anti-spam system
Upvotes: 4