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

Discord.py中{0.author}失效时能否强制机器人返回带用户名的Embed消息

Discord.py 嵌入消息显示触发用户名称解决方法

你的代码无法正常显示用户名的核心原因是:字符串中的{0.author}占位符没有执行格式化逻辑,会被当成普通文本直接输出,该功能可以直接实现,无需额外的“强制”操作。

修正方案

你可以选择以下任意一种方式补全字符串格式化逻辑即可:

方案1:使用Python f-string(推荐,写法更简洁)

这是Python 3.6及以上版本支持的语法,直接在字符串前加f即可插入变量:

if message.content.startswith('Привет'):
    print('[command]: Задан вопрос3 ')
    embed = discord.Embed(color=0x9d00ff)
    # display_name 会优先显示用户在当前服务器的昵称,没有则显示全局用户名
    embed.title = f'**Привет {message.author.display_name}!**' 
    # 如果需要@触发用户,点击可跳转用户资料,使用mention属性
    embed.description = f'**Привет {message.author.mention}!**'
    await message.channel.send(embed=embed)

方案2:兼容原有写法的format方法

如果你要保留原有的{0}占位符写法,只需要在字符串末尾调用.format()方法传入message对象即可:

if message.content.startswith('Привет'):
    print('[command]: Задан вопрос3 ')
    embed = discord.Embed(color=0x9d00ff)
    embed.title = '**Привет {0.author.display_name}!**'.format(message) 
    embed.description = '**Привет {0.author.mention}!**'.format(message)
    await message.channel.send(embed=embed)

注意事项

如果你的机器人无法触发该回复逻辑,请确认你已经在Discord开发者平台的机器人设置页开启了Message Content Intent权限,且代码中初始化客户端时已经注册了该权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 17:18:03