Discord机器人斜杠命令执行成功却仍提示‘应用未响应’求助
Discord斜杠命令触发"The application did not respond"的解决方法
问题根源
Discord对斜杠命令有严格的响应时限要求:机器人必须在3秒内返回一个初始确认响应,否则会判定为“应用未响应”。你的代码中使用ctx.send()发送消息,但这个方法属于后续跟进消息,无法替代斜杠命令要求的初始响应——因此即便角色操作功能正常执行,Discord仍会弹出提示。
修复代码
将ctx.send()替换为ctx.respond()作为初始响应,这是discord.py中处理斜杠命令的标准方式:
# Verify command @bot.slash_command() async def verify(ctx): # 发送初始响应,告知Discord命令已接收并处理 await ctx.respond(f"Welcome to the Galaxy!", delete_after=10) member_role = discord.utils.get(ctx.guild.roles, id=891110682110623806) unverified_role = discord.utils.get(ctx.guild.roles, id=1012034653613477961) await ctx.author.add_roles(member_role) await ctx.author.remove_roles(unverified_role)
进阶优化
如果角色操作可能耗时较长(比如服务器角色数量多),可以先发送临时加载提示,再用跟进消息更新结果:
@bot.slash_command() async def verify(ctx): # 仅对发起者可见的临时响应 await ctx.respond("正在验证身份...", ephemeral=True) member_role = discord.utils.get(ctx.guild.roles, id=891110682110623806) unverified_role = discord.utils.get(ctx.guild.roles, id=1012034653613477961) await ctx.author.add_roles(member_role) await ctx.author.remove_roles(unverified_role) # 发送最终结果的跟进消息 await ctx.send_followup(f"Welcome to the Galaxy!", delete_after=10)
内容的提问来源于stack exchange,提问作者Fraser
相关产品推荐
相关产品推荐

