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

discord.py中@提及功能失效:Embed嵌入消息内无法正常显示@用户名

问题原因

Discord 本身不支持在 Embed 的 footer、author 名称、标题等附属位置解析 <@用户ID> 格式的提及语法,只有 Embed 的正文(description)、字段(fields)的名称和内容区域会对该格式进行渲染,且就算渲染为可点击的用户卡片,也不会触发用户的 @ 提醒通知。

解决方案

根据你的使用场景可以选择以下两种处理方式:

直接拉取用户对象获取用户名填入 footer 即可,无需使用提及格式:

@client.command()
async def user(ctx, subcommand, name):
    player = osu.fetch_user(username=name)
    if subcommand == "stats":
        embed = discord.Embed(title=f"osu! stats of {name}")
        owner_id = 416651818760929291
        # 拉取用户对象获取用户名
        owner = await client.fetch_user(owner_id)
        embed.set_footer(
            text=f"Shuka (osu! bot) by {owner.name}",
            icon_url="https://i.pinimg.com/originals/92/f7/cb/92f7cbba6e1f629f619ad878066ededc.png"
        )
        await ctx.send(embed=embed)

需要显示可点击的用户提及卡片/触发@提醒

  • 若仅需要可点击的用户卡片,将提及语法放到 Embed 的 description 或 fields 区域即可
  • 若需要触发用户的@提醒,必须将提及语法放到 ctx.send() 的 content 参数中
    修改后的参考代码如下:
from discord import AllowedMentions

@client.command()
async def user(ctx, subcommand, name):
    player = osu.fetch_user(username=name)
    if subcommand == "stats":
        owner_id = 416651818760929291
        embed = discord.Embed(
            title=f"osu! stats of {name}",
            description=f"Bot developed by <@{owner_id}>"
        )
        embed.set_footer(
            text=f"Shuka (osu! bot)",
            icon_url="https://i.pinimg.com/originals/92/f7/cb/92f7cbba6e1f629f619ad878066ededc.png"
        )
        # 配置允许提及的范围,避免误@风险
        allowed_mentions = AllowedMentions(users=[owner_id], roles=False, everyone=False)
        # content参数放提及才会触发@通知
        await ctx.send(content=f"<@{owner_id}>", embed=embed, allowed_mentions=allowed_mentions)
注意事项
  • 不要在 Embed 的 footer、author、title 区域使用任何提及语法,该类位置不会被解析
  • 若不需要触发@通知,可以省略 content 参数,仅在 Embed 正文区域放提及语法即可

内容的提问来源于stack exchange,提问作者Adachi Sakura

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 03:45:03