(discord.py) 如何在 DM 中显示机器人输入指示器

IAm*_*man 2 python python-3.x discord discord.py

我听说ctx.typing()存在,但我希望我的机器人在私信中显示该指标。到目前为止,这是我的代码:

@client.event
async def on_message(message):
    await client.process_commands(message)
    # i want the bot to display 'typing...' for 1 second
    if message.guild is None and not message.author.bot:
        with open('dmresponses.txt') as input_file:
            long_list = [line.strip() for line in input_file]
        await message.author.send(random.choice(long_list))
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现这个?

Dig*_*gy. 5

这可以通过上下文管理器来实现typing()

@client.event
async def on_message(message):
    await client.process_commands(message)
    if message.guild is None and not message.author.bot:
        async with message.channel.typing():
            with open("dmresponses.txt") as input_file: # do stuff inside
                long_list = [line.strip() for lin in input_file]
        await message.author.send(random.choice(long_list)) # what to do after typing
Run Code Online (Sandbox Code Playgroud)

如果您想强调打字持续时间,您可以使用以下命令添加延迟asyncio.sleep()

import asyncio # required for the sleeping

async with message.channel.typing():
    await asyncio.sleep(0.5)
    # some stuff
await message.channel.send("Done!")
Run Code Online (Sandbox Code Playgroud)

参考: