【问题标题】:Python Discord Bot - Get a users roles in a different serverPython Discord Bot - 在不同的服务器中获取用户角色
【发布时间】:2020-08-09 11:23:08
【问题描述】:

所以我可以使用以下代码检查用户在服务器中的角色:

        #Check Admin:
        playerRoles = []
        for x in message.author.roles:
            playerRoles.append(str(x))
        if "Admin" in playerRoles:

但我希望机器人能够检测到它们在与发出命令的服务器不同的服务器中所具有的角色。我希望能够通过 DM 将命令发送给机器人,所以显然我不能使用 message.author.roles 因为用户在 DM 中没有角色,他有服务器中的角色。

这是我尝试过的:

        #Check Roles:
        playerRoles = []
        #target the user manually to get his roles in the event of a DM
        pepeServer = client.get_guild(guildID)
        targetUser = discord.utils.get(pepeServer.members, id=userID)
        for x in targetUser.roles:
            playerRoles.append(str(x))

这会在“for x in targetUser.roles:”行返回错误

AttributeError: 'NoneType' 对象没有属性 'roles'

我猜它获得的“targetUser”不是具有角色的那种。感觉很糟糕。如何从 DM 中发出的命令获取用户在服务器中的角色?

感谢您的帮助

【问题讨论】:

    标签: python bots discord roles dm


    【解决方案1】:

    AttributeError: 'NoneType' object has no attribute 'roles' 表示targetUserNone

    所以你的discord.utils.get(pepeServer.members, id=userID) 不会返回任何东西。

    检查服务器和用户 ID。

    【讨论】:

    • 哇,我没有意识到 xD 用户 ID 是一个字符串而不是一个整数,我感到很愚蠢。第一次这样做,所以我只是假设我的方法是错误的。感谢您的帮助
    猜你喜欢
    • 2020-11-23
    • 1970-01-01
    • 2020-07-31
    • 2023-01-26
    • 2020-08-16
    • 2022-01-05
    • 2023-03-22
    • 2021-04-15
    • 2020-12-26
    相关资源
    最近更新 更多