1

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

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

但我希望 bot 能够检测到它们在不同于发出命令的服务器中的角色。我希望能够通过将命令发送给机器人来发出命令,所以显然我不能使用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”对象没有属性“角色”

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

谢谢您的帮助

4

1 回答 1

1

AttributeError: 'NoneType' object has no attribute 'roles'意思targetUserNone

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

检查服务器和用户 ID。

于 2020-04-25T21:48:17.080 回答