Discord混合命令中斜杠命令无法正确获取member.status问题求助
Discord混合命令斜杠调用无法获取用户在线状态的问题解决
编写了如下Discord混合命令:
@commands.hybrid_command() async def test(self, ctx: commands.Context): await ctx.send(f"Your status: {ctx.author.status}")普通命令
!test调用时能正确返回用户状态,但斜杠命令调用时始终显示"offline"。所有斜杠命令都无法正确获取member.status,而普通commands.command()命令正常工作。已设置discord.Intents.all()并重置开发者门户权限,补充测试发现用ctx.guild.get_member(ctx.author)能正常获取状态,推测斜杠命令中ctx.author的对象存在异常。
问题原因
斜杠命令场景下,ctx.author返回的是User对象,而非普通命令中的Member对象。User是全局用户实体,不包含服务器内的在线状态数据,因此默认显示为offline;而Member是用户在特定服务器内的实例,包含该服务器内的状态信息,普通命令触发时框架默认传递Member对象,所以能正确获取状态。
解决方案
方法1:从服务器中获取Member实例
通过ctx.guild.get_member(ctx.author.id)将User对象转换为当前服务器的Member对象,再获取状态。
@commands.hybrid_command() async def test(self, ctx: commands.Context): member = ctx.guild.get_member(ctx.author.id) if member: await ctx.send(f"Your status: {member.status}") else: await ctx.send("无法获取你的服务器状态信息")
方法2:通过参数注解直接接收Member对象
在命令参数中指定discord.Member类型,并默认绑定调用者,框架会自动在斜杠命令中解析为Member对象。
@commands.hybrid_command() async def test(self, ctx: commands.Context, member: discord.Member = commands.Author): await ctx.send(f"Your status: {member.status}")
注意事项
确保机器人已开启members intent(你已设置Intents.all(),此条件已满足),否则get_member可能返回None。
内容的提问来源于stack exchange,提问作者foxtrot
相关产品推荐
相关产品推荐

