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

AttributeError:Context对象无nickname属性,如何获取该属性?

解决"context object no nickname attribute"错误并获取昵称
  • Context对象本身并不包含nickname属性,用户的昵称信息存储在与Context关联的用户资料实例中。
  • 在edamino框架中,你可以通过ctx.user.nickname来获取发送命令用户的昵称(部分版本可能需要使用ctx.sender.nickname,可根据实际情况测试)。

修改后的on_ping命令代码示例:

@bot.command('ping', prefix="!")
async def on_ping(ctx: Context):
    # 获取发送命令用户的昵称
    user_nickname = ctx.user.nickname
    await ctx.reply(f"pong! {user_nickname}")
  • 若要获取机器人自身的昵称,可在on_ready事件中通过传入的profile参数直接获取:
@bot.event()
async def on_ready(profile: UserProfile):
    bot_nickname = profile.nickname
    logger.info(f'{bot_nickname} 已启动')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 14:18:10