如何使Discord机器人显示“机器人正在键入…”状态?

2024-06-25 23:47:55 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,如果我有这样一个长命令:

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

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


Tags: 命令truetypingasyncdefbotstatuscontext
3条回答

如果您使用重写分支,那么所有Messageable都有一个^{}上下文管理器,允许您无限期地键入,还有一个^{}协同例程,显示键入消息几秒钟

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

这对我有用! Im使用Discord.py(不重写)

编辑:较新版本的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

相关问题 更多 >