Python Discord Bot在不同的服务器上获取用户角色

2024-10-01 00:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

但是我希望bot能够检测到它们在与发出命令的服务器不同的服务器中的角色。我希望命令能够通过将其管理到bot来发出,因此显然我不能使用message.author.roles,因为用户在DMs中没有角色,所以他在服务器中有角色

以下是我尝试过的:

        #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))

这将在targetUser.roles中的“for x”行返回错误:

AttributeError:“非类型”对象没有属性“角色”

我猜它获得的“目标用户”不是那种有角色的人。感觉坏蛋。如何从DMs中发出的命令中获取用户在服务器中的角色

谢谢你的帮助


Tags: 用户in命令服务器角色messageforget
1条回答
网友
1楼 · 发布于 2024-10-01 00:22:55

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

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

检查服务器和用户id

相关问题 更多 >