Search code examples
pythonbotsdiscordrolesdm

Python Discord Bot - Get a users roles in a different server


So I can check a user's roles in a server with the following code:

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

But I want bot to be able to detect the roles they have in a different server than where the command was issued. I want the command to be able to be issued by DMing it to the bot, so obviously I can't use message.author.roles because the user doesn't have roles in DMs, he has the role in the server.

Here's what I tried:

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

This returns an error on the line "for x in targetUser.roles:"

AttributeError: 'NoneType' object has no attribute 'roles'

I guess the "targetUser" it's obtaining isn't the kind with roles. Feelsbadman. How do I get the user's roles in the server from a command issued in DMs?

Thanks for the help


Solution

  • AttributeError: 'NoneType' object has no attribute 'roles' means that targetUser is None.

    So your discord.utils.get(pepeServer.members, id=userID) doesnt return anything.

    Check the server and user id.