You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py编写发送提及用户头像Embed命令的问题咨询

discord.py Embed展示提及用户头像实现方案

现有代码问题

当前代码仅将用户头像的URL作为纯文本放到了Embed的描述字段中,没有调用Embed的图片配置接口,因此无法直接在消息卡片内显示头像图片,原有代码片段:

@client.command()
async def v1_3(ctx, user: discord.User):
    avurl = user.avatar_url
    embed=discord.Embed(title=f"{user.name}", description=f"{avurl}", color=0xf20202)
    await ctx.send(embed=embed)

修正方法

根据需要的头像展示位置,选择对应的Embed配置方法即可:

  • 展示在Embed右上角缩略图位置(最常用的头像展示位)
    调用set_thumbnail()方法传入头像链接,修正后可直接运行的代码:
    @client.command()
    async def v1_3(ctx, user: discord.User):
        # 兼容discord.py 1.x旧版本和2.x新版本的头像链接获取逻辑
        avurl = user.avatar.url if hasattr(user, 'avatar') else user.avatar_url
        embed = discord.Embed(title=f"{user.name}", color=0xf20202)
        embed.set_thumbnail(url=avurl)
        await ctx.send(embed=embed)
    
  • 展示在Embed底部大图片位置
    将上述代码中的set_thumbnail替换为set_image即可:
    embed.set_image(url=avurl)
    
  • 展示在Embed标题左侧的作者栏图标位置
    使用set_author方法同时配置作者名和头像图标:
    embed.set_author(name=user.name, icon_url=avurl)
    

版本提示:discord.py 2.0及以上版本已经移除了user.avatar_url属性,直接使用旧写法会触发属性不存在的报错,加上简单的兼容判断可以适配所有主流版本。

内容的提问来源于stack exchange,提问作者Bruh John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 13:24:29