python - 向 Python 中的特定角色发送私有(private)消息

标签 python discord.py

我如何向 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/

相关文章:

python - Ansible 使用解析字符串和循环更新 Dict Var

python - 如何让 grabCut 与 GC_INIT_WITH_MASK 一起工作 opencv python

python - 具有不同形状的 Tensorflow 元组

python-3.x - 如何让我的 discord.py 机器人在语音 channel 中播放 mp3?

python - Discord.py 的 "on_message"可以将 't work after I implemented "schedule"写入其中

python - 从设置的用户获取个人资料图片

python - 在 Pandas Dataframe 中进行分组时的多重聚合

Python字符串模板替换为正则表达式组的字典?

python-3.x - 机器人两次执行相同的命令

python - 从discord.py rewrite 发送一个pm