如何让 Discord 机器人显示“Bot is typing ...”状态?

新手上路,请多包涵

所以如果我有一个像这样的长命令:

 @bot.command(pass_context=True)
async def longCommand(ctx):
   #typing status
   sleep(10)
   bot.say("Done!")

不幸的是,在文档或此处没有找到任何内容。

原文由 David Fischer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 398
2 个回答

编辑:较新版本的 discord 要求您使用新语法:

 @bot.command()
async def mycommand(ctx):
    async with ctx.typing():
        # do expensive stuff here
        await asyncio.sleep(10)
    await ctx.send('done!')

旧版本使用这个:

 @bot.command(pass_context=True)
async def longCommand(ctx):
   await bot.send_typing(ctx.channel)
   await asyncio.sleep(10)
   await bot.say("Done!")

请记住在每次异步调用协程时使用 await

原文由 nosklo 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您使用重写分支,那么所有 Messageable 都有一个 typing 允许您无限期键入的上下文管理器,以及一个 trigger_typing 消息显示协程几秒钟。

 @bot.command()
async def longCommand(ctx):
   async with ctx.typing():
        await sleep(10)
   await ctx.send("Done!")

原文由 Patrick Haugh 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题