gpt4 book ai didi

python - 如何让我的 Discord 机器人看起来像是在打字?

转载 作者:行者123 更新时间:2023-12-04 09:44:04 25 4
gpt4 key购买 nike

我有一个运行 discord.py 的 Discord 机器人,当我使用帮助功能时,消息之间有时会有几秒钟的小延迟。当一个活着的用户在 Discord 上打字时,它通常会在说 Example 正在打字时显示 3 个小点交替的阴影。我怎样才能让这个显示在我的机器人上?
我有一个现在不起作用的功能,它是


async def balabala (ctx):
async with ctx.typing():
await message.channel.send(prefix + "random [Value 1] [Value 2] - Gives a random number between Value 1 and Value 2, inclusive")
await ctx.send(prefix + "choose [Value 1] (Value 2) (Value 3) etc. - Chooses a random value from the list of values you provide") # what you want to do after typing
@client.event
async def on_message(message):
if message.content.lower() == prefix + "help":
await message.channel.send("The prefix for this bot is " + prefix)
await message.channel.send("Note: Do not actually enter [](required) or ()(Not required)")
await message.channel.send(prefix + "help - Shows this message" )
await message.channel.send("Hello - Gives a hello back, because you are so nice")
await message.channel.send(prefix + "invite - Gives an invite to the bot, and Canary versions")

await balabala()
出现的错误是
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "discordbotcanary.py", line 45, in on_message
await balabala()
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 354, in __call__
return await self.callback(*args, **kwargs)
TypeError: balabala() missing 1 required positional argument: 'ctx'

最佳答案

您将能够打开 Messageable 对象(例如 TextChannel、DMChannel)的上下文管理器并在其中执行您的任务。

我正在使用 asyncio.sleep()以延迟为例:

@bot.command()
async def hello(ctx):
async with ctx.typing():
await asyncio.sleep(2) # some tasks go here, the sleep is just for a delay
await ctx.send(f"Hello, {ctx.author.mention}!") # what you want to do after typing

引用文献:
  • Messageable.typing()
  • asyncio.sleep()
  • abc.Messageable
  • commands.Context - async with typing()在这里工作是因为它“实现了 Messageable ABC”。
  • 关于python - 如何让我的 Discord 机器人看起来像是在打字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62199232/

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