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
相关产品推荐
相关产品推荐

