如何使用discord.py获取Discord用户的About Me(个人简介)
通过discord.py获取Discord用户About Me字段的可行方案
前置要求
首先确保满足以下两个条件:
- 你正在使用 discord.py v2.0 及以上版本,低于v2.0的旧版本未内置对应公开属性,需要自行适配底层接口,推荐优先升级版本
- 已在Discord开发者平台的机器人管理后台开启
Presence Intent和Server Members Intent两个权限,否则无法获取用户的完整个人信息
方案1:直接读取内置属性
discord.py v2.x 已将用户简介字段封装为公开属性,可直接通过 User.bio 或 Member.bio 获取:
# 斜杠命令示例:获取指定用户的简介 @bot.tree.command(name="get_bio", description="查询用户的个人简介") async def get_bio(interaction: discord.Interaction, target_user: discord.User): bio_content = target_user.bio if target_user.bio is not None else "该用户暂未填写个人简介" await interaction.response.send_message(f"用户 {target_user.name} 的个人简介:\n{bio_content}")
注意:如果拿到的bio为None,排除用户未填写的情况,大概率是机器人没有拿到对应权限,或本地缓存未同步用户的完整数据。
方案2:主动拉取最新用户资料
如果本地缓存没有用户的简介数据,可以主动调用拉取接口获取最新的用户完整信息:
# 斜杠命令示例:主动拉取用户最新简介 @bot.tree.command(name="fetch_bio", description="主动拉取用户最新的个人简介") async def fetch_bio(interaction: discord.Interaction, target_user: discord.User): # 调用接口拉取最新用户数据,不走本地缓存 fresh_user_info = await bot.fetch_user(target_user.id) bio_content = fresh_user_info.bio if fresh_user_info.bio is not None else "该用户暂未填写个人简介" await interaction.response.send_message(f"用户 {fresh_user_info.name} 的个人简介:\n{bio_content}")
如果查询的是当前服务器内的成员,也可以调用 await guild.fetch_member(target_user.id) 拉取成员数据,返回的对象中同样包含bio属性。
内容的提问来源于stack exchange,提问作者guyoneguytwo
相关产品推荐
相关产品推荐

