gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:26:50 34 4
gpt4 key购买 nike

我听说 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))

我该如何实现这个?

最佳答案

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

引用文献:

关于python - (discord.py) 如何在 DM 中显示机器人输入指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62311644/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com