pythonpython-3.xdiscord.py

Getting profile badges (Discord.py)


I want to get the badges of a discord user using discord.py. I am currently using Python 3.12.6 and the latest version of discord.py. The method that I'm using right now is that I have a function that takes in a message object and takes the first mentioned user in that message and uses it as the user to fetch the info from. What I have right now is this:

user = message.mentions[0] # Gets the user from who to take the info from
badges  = user.public_flags # Gets the badges
badges_str = str(badges.all()).replace('[<UserFlags.', '').replace('>]', '').replace('_',' ').replace(':', '').title() # Replaces not needed info
badges_str = ''.join([i for i in badges_str if not i.isdigit()]) # removes the numbers

Now I have also tried to print all the values, including badges, but all of it just does not work. The user is on the guild that this command is being run on, and I have many exceptions to confirm that the user exists, and the program is fetching the correct person.

All that I really want is to get a list of badge Objects or strings that I can link to icons to show the badges in a nice little embed. But all that I'm getting is an empty array instead. There is no information and I even tried it on other servers, roles, bots, tokens, intents, etc...

I tried to read the docs (https://discordpy.readthedocs.io/en/) and I tried to look at many other stack overflow questions and look at other Python projects on GitHub that are doing this or something similar. All that, and down to reading the source code of discord.py. Nothing helped, and I am coming here to hopefully find an answer to a relatively weird question.


Solution

  • So user.public_flags.all() is actually a list of Enums. Rather than converting the whole list of enums to a string and stripping a bunch of extraneous stuff, you might find more success using it as the data it is, e.g.:

    user = message.mentions[0]
    badges = [badge.name for badge in user.public_flags.all()]