【问题标题】:Discord.py Warning Command (help me) mass dmDiscord.py 警告命令(帮助我)mass dm
【发布时间】:2020-03-31 00:26:05
【问题描述】:

几天前我试图创建一个警告命令,一个 dm mass 类型,但不是发送给所有服务器用户,而是只发送几个人到一个特定的角色。

我只能创建一个批量 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')

【问题讨论】:

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


    【解决方案1】:

    在 Discord.py rewrite 的文档中,您确实可以找到所有内容。包括成员对象具有的对象: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")
    

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 1970-01-01
      • 2020-11-06
      • 2021-09-16
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多