Pycord斜杠命令/chat执行后,如何消除机器人的pending状态?
解决Discord机器人/chat命令pending提示持续存在的问题
问题出在你调用了await ctx.response.defer(ephemeral=True)延迟响应后,没有发送最终的完成信号,Discord会一直认为交互未结束,所以"bot is thinking"的提示不会消失。
只需在完成线程创建和消息发送后,添加一行代码结束交互即可:
import discord from tools.config import TOKEN from tools.gpt_helper.gpt_custom_classes import generate_response from discord.ext import commands bot = discord.Bot() @bot.event async def on_ready(): print(f"{bot.user} is ready and online!") @bot.slash_command(name="chat", description="some desc") async def chat(ctx, msg): channel = bot.get_channel(MY_CHANNEL_ID) await ctx.response.defer(ephemeral=True) resp = await generate_response(prompt=msg) thread = await channel.create_thread(name="wop", type=None) await thread.send(resp) # 发送最终响应,终止pending状态 await ctx.followup.send("对话已在新线程中启动!", ephemeral=True) bot.run(TOKEN)
如果不想显示提示文本,也可以用空内容完成交互:
await ctx.followup.send("", ephemeral=True)
注:使用defer是为了避免命令因生成回复耗时过长而超时,这个逻辑是合理的,只需补充后续的完成响应即可。
内容的提问来源于stack exchange,提问作者syrok
相关产品推荐
相关产品推荐

