我如何向 Python 中的特定角色发送私有(private)消息
这是我已经完成的:
@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')
最佳答案
user.roles
返回一个列表,但是您只是将它与单个 role
对象进行比较。这将始终为 False
。相反,检查所需的角色是否在 user.roles
中。如果是,请检查该用户是否已经存在 DM,否则创建一个并发送消息。
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)
然而,您还应该包括延迟,以免被 discord 反垃圾邮件系统标记
关于python - 向 Python 中的特定角色发送私有(private)消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67670487/