获取个人资料徽章(Discord.py)

问题描述 投票:0回答:1

我想使用discord.py 获取discord 用户的徽章。我目前正在使用

Python 3.12.6
和最新版本的discord.py。我现在使用的方法是,我有一个函数,该函数接收消息对象并获取该消息中第一个提到的用户,并将其用作从中获取信息的用户。我现在拥有的是这样的:

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

现在我也尝试打印所有值,包括

badges
但所有这些都不起作用。用户位于运行此命令的行会中,并且我有许多异常句柄来确认用户存在并且程序正在获取正确的人。

我真正想要的是获得

badge Objects
strings
的列表,我可以链接到图标以在漂亮的小嵌入中显示徽章。但我得到的只是一个空数组。没有信息,我什至在其他服务器、角色、机器人、令牌、意图等中尝试过......

我尝试阅读文档(https://discordpy.readthedocs.io/en/),并尝试查看许多其他堆栈溢出问题,并查看 GitHub 上正在执行此操作或类似操作的其他 Python 项目。所有这些,最后就是阅读discord.py 的源代码。没有任何帮助,我来这里希望找到一个相对奇怪的问题的答案。

python python-3.x discord discord.py
1个回答
0
投票

所以

user.public_flags.all()
实际上是
list
Enum
。与其将整个枚举列表转换为字符串并删除一堆无关的东西,您可能会发现将其用作数据会更成功,例如:


user = message.mentions[0]
badges = [badge.name for badge in user.public_flags.all()]
© www.soinside.com 2019 - 2024. All rights reserved.