【问题标题】:How to give a role to a member with custom status on discord.py如何在 discord.py 上为具有自定义状态的成员赋予角色
【发布时间】:2021-11-02 20:36:21
【问题描述】:

我是 discord.py 的新手,我想知道是否可以为成员提供特定自定义状态的角色,以及是否可以在他们删除后自动删除它?

【问题讨论】:

    标签: discord discord.py


    【解决方案1】:

    您可以遍历公会的所有成员。 Guild 指的是一个不和谐的服务器。 您可以使用角色 id 获取角色:

    # define guild and role_id variables
    role = guild.get_role(role_id)
    

    然后您可以使用以下命令遍历公会的所有成员:

    #define required_activity
    for user in guild.members:
        userHasActivity = false
        for activity in user.activities:
            if isinstance(activity, discord.CustomActivity):
                if str(activity) == required_activity:
                    userHasActivity = true
        if userHasActivity:
            await user.add_roles([role])
        else:
            await user.remove_roles(role)
    
    

    如果将其用作命令,则可以使用ctx.guild 获得公会,它指的是运行该命令的公会。您可以使用bot.get_guild 使用服务器 ID 获得公会。您还可以使用bot.guilds 获取机器人所在的所有公会。但是,不同公会的角色 ID 不会相同,因此您必须注意正确存储和设置角色 ID。

    它可以重构为一个单独的async 函数并使用discord.ext.tasks 自动化。

    请注意,对于大型服务器,这可能会很慢。

    注意事项:

    1. 请注意,对于大型服务器,这可能会很慢。

    2. 上面的代码没有任何错误检查,所以您需要添加它。使用 try-catch 块来捕获错误。如何处理错误取决于您:记录错误、忽略错误等。

    3. remove_roles 函数的语法在 discord.py 1.7.3 的修改版本上进行了测试并且可以工作,但根据文档,代码应该是 await user.remove_roles([role])。根据 discord.py 版本,这可能会有所不同。

    【讨论】:

    • 谢谢它的工作,但它似乎并没有删除角色任何帮助?
    • 寻求帮助和让别人为你做这一切并给你工作代码是有区别的。先自己试一试。
    • 我尝试添加 else 语句,但它不起作用,如果你能帮助我,那就太好了。
    • @Turtle_god 如果您包括如何添加 else 语句以及它如何不起作用,将会很有帮助。另请参阅最后一段:注意事项。
    【解决方案2】:

    你可以这样做:

    @bot.command()
    async def role(ctx):
        role = discord.utils.get(ctx.guild.roles, name="role_name")
        # this gets the role (replace the "role name" with the actual role name)
        user = ctx.message.author
        # this gets the user
        if (user.activities == 'status'):
            # this checks if his status is 'status'
            await user.add_roles(role)
            # this grants the user the role
    

    【讨论】:

      【解决方案3】:

      最好在会员更新时检查一下。每次有人更改他们的活动时,它都会运行。在了解 Suven Pandey 提到的 role 后,您可以使用以下代码:

      @bot.event
      async def on_member_update(before, after):
      
          if after.bot == True:
              return
      
          if before.activities == after.activities:
              return
      
          # define required_activity
          userHasActivity = False
          for activity in after.activities:
              if isinstance(activity, discord.CustomActivity):
                  if str(activity) == required_activity:
                      userHasActivity = True
          if userHasActivity:
              await after.add_roles([role])
          else:
              await after.remove_roles([role])
      

      它基本上是从 Suven Pandey 复制的,只是适应了on_member_update。我认为这与您正在寻找的一样接近。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 2021-10-11
        • 2020-12-05
        • 1970-01-01
        • 2021-07-01
        相关资源
        最近更新 更多