[英](discord.py) How to display bot typing indicator in DMs
I heard that ctx.typing()
exists but I want my bot to display the indicator in DMs.我听说
ctx.typing()
存在,但我希望我的机器人在 DM 中显示指示器。 Here is my code so far:到目前为止,这是我的代码:
@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))
How can I implement this?我该如何实施?
This can be achieved with the typing()
context manager:这可以通过
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
And if you want to emphasize the typing duration, you can include a delay by using asyncio.sleep()
:如果你想强调输入持续时间,你可以使用
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!")
References:参考:
Messageable.typing()
- Messageable is somewhere that you can send a message (Text channel, DM, group chat etc.). Messageable.typing()
- Messageable 是您可以发送消息的地方(文本频道、DM、群聊等)。 message.channel
is a discord.TextChannel
object message.channel
是discord.TextChannel
objectabc.Messageable
discord.TextChannel
asyncio.sleep()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.