【问题标题】:(discord.py) How to display bot typing indicator in DMs(discord.py) 如何在 DM 中显示机器人输入指示器
【发布时间】:2020-09-30 08:16:32
【问题描述】:

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

@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))

我该如何实现?

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    这可以通过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
    

    如果你想强调输入持续时间,你可以使用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!")
    

    参考资料:

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 2023-03-31
      • 2021-06-12
      • 2018-12-17
      • 2018-05-10
      • 2021-02-12
      • 2019-05-07
      • 2021-04-30
      • 1970-01-01
      相关资源
      最近更新 更多