【问题标题】:Discord-py When a user is typing, how to make the bot to follow the typing and when the user stop, the bot also stop typing in a specific channel?Discord-py 当用户打字时,如何让机器人跟随打字,当用户停止时,机器人也停止在特定频道输入?
【发布时间】:2021-05-05 08:57:24
【问题描述】:

当用户打字时,如何让机器人在某个频道也启动打字事件,并在用户打字停止时停止。

ctx.trigger_typing/ctx.typing 无法使用,因为我们需要跟踪用户是否正在打字。

那该怎么做呢?

【问题讨论】:

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


【解决方案1】:

编辑:

启用 intents.typing 并在 Discord 开发者门户中打开意图可以访问 on_typing 事件,您只需启用代码中的所有意图,例如:

intents = discord.Intents.all()
client = discord.Client(intents=intents)

打字事件可以简单地使用on_typing事件来调用,

@client.event
async def on_typing(channel, user, when):
    print(f"{user} is typing message in {channel} {when}")

如果你想让你的机器人有一个打字状态,你可以使用ctx.typing():,后跟它会发送的内容。

这是一个on_message 事件示例,

@client.event
async def on_message(message):
    async with message.channel.typing():
        await message.channel.send('Typing status invoked')
        await client.process_commands(message)

命令用法,还可以添加睡眠事件来控制打字时间

@client.command()
async def type(ctx):
    async with ctx.typing():
        await asyncio.sleep(2)
    await ctx.send("Typing...")

【讨论】:

  • on_typing 事件怎么样?
  • 对不起,我需要更新,我找到了一些关于打字事件的信息,谢谢@ŁukaszKwieciński
  • 抱歉,我只是不知道 Discord 的 API 中存在这个,希望对您有所帮助
  • 但是如何跟踪用户以查看他/她是否正在打字?比如用户在打字的时候,bot在某个频道也应该有打字状态,当用户停止时,bot也应该停止,怎么做
猜你喜欢
  • 2022-01-21
  • 2021-10-12
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2021-02-25
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多