Discord.py bot生日祝福命令发送embed消息时@提及用户ping失效
问题根因
Discord 原生规则限制:所有放在Embed结构体内部的用户、角色、频道提及都不会触发提醒通知,也不会显示为高亮格式,只有放在消息普通内容(content字段)中的提及才会触发对应效果。
修复方案
方案1:同时实现高亮+提醒
将用户提及放在send方法的content参数中,Embed内可以保留原格式,发送后正文的提及会高亮且触发ping:
@bot.command() # NUMBER 5 async def hbd(ctx, user: discord.User): target = user em = discord.Embed(title=f"Happy Birthday!", description=f"Happy Birthday {target.mention}", color=discord.Color.green()) em.set_footer(text=f"Wishes from {ctx.author}", icon_url=ctx.author.avatar_url) # 把提及放到消息正文触发高亮和提醒 await ctx.channel.send(content=target.mention, embed=em)
方案2:仅触发提醒、正文不显示提及
如果不想在正文显示@内容,只要用户收到ping,可以通过allowed_mentions参数指定要提醒的用户,无需修改Embed内容:
@bot.command() # NUMBER 5 async def hbd(ctx, user: discord.User): target = user em = discord.Embed(title=f"Happy Birthday!", description=f"Happy Birthday {target.mention}", color=discord.Color.green()) em.set_footer(text=f"Wishes from {ctx.author}", icon_url=ctx.author.avatar_url) # 显式指定允许提及的用户,即使正文没有显示提及也会触发ping await ctx.channel.send(embed=em, allowed_mentions=discord.AllowedMentions(users=[target]))
小提示:如果你使用的是discord.py 2.0及以上版本,代码中的
ctx.author.avatar_url需要修改为ctx.author.avatar.url,否则会调用失效报错。
内容的提问来源于stack exchange,提问作者DavyJonesStockings
相关产品推荐
相关产品推荐

