discord.py中@提及功能失效:Embed嵌入消息内无法正常显示@用户名
问题原因
Discord 本身不支持在 Embed 的 footer、author 名称、标题等附属位置解析 <@用户ID> 格式的提及语法,只有 Embed 的正文(description)、字段(fields)的名称和内容区域会对该格式进行渲染,且就算渲染为可点击的用户卡片,也不会触发用户的 @ 提醒通知。
解决方案
根据你的使用场景可以选择以下两种处理方式:
仅需要在 footer 显示开发者名称
直接拉取用户对象获取用户名填入 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
相关产品推荐
相关产品推荐

