Discord.py警告命令(帮助我)mass dm

时间:2019-12-06 02:10:16

标签: python python-3.x command discord.py discord.py-rewrite

几天前,我试图创建一个警告命令,即dm质量类型,但是与其将其发送给所有服务器用户,还不如将其发送给所有服务器用户。

我只能创建一个质量dm命令

        @commands.command()
    async def all(self, ctx,*,message):
        for mem in ctx.guild.members:
            try:
                await mem.send(message)
                await ctx.send(f'**Sent dm to:** {mem.name}')
            except:
                await ctx.send(f'**User dm closed** {mem.name}')
                print('User dm closed')

1 个答案:

答案 0 :(得分:0)

在Discord.py重写的文档中,您可以真正找到所有内容。包括成员对象具有的对象:https://discordpy.readthedocs.io/en/latest/api.html?highlight=member#member。阅读文档时,您会看到可以使用mem.roles来获取成员拥有的角色对象的列表。

您可以遍历用户具有的角色列表,并检查角色的字符串值是否等于所需的角色。如果是所需角色,则可以向该成员发送消息。

您需要执行str(role) == desired_role而不是role == desired_role的原因是role是一个对象。而且您无法将对象与字符串进行比较。

@commands.command()
async def message_user_w_role(self, ctx, desired_role):
    # For every member in the guild in which the message was send.
    for mem in ctx.guild.members:
        # Members.roles gives list with roles. Thus you need to iterate and check if role == desired role
        for role in mem.roles:
            if str(role) == desired_role:
                try:
                    # Sends the dm if the user has the desired role
                    await mem.send("YOUR MESSAGE")
                    await ctx.send("ANOTHER MESSAGE")
                    break
                except:
                    # If the user closed his dm's
                    await ctx.send("ANOTHER MESSAGE")
                    print("User dm closed")