我有一个运行discord.py的Discord机器人,当我使用帮助功能时,消息之间有时会延迟几秒钟。当有生命的用户在Discord上打字时,通常会显示3个小点交替的阴影,并说出Example打字。我该如何在我的机器人上显示出来? 我有一个暂时无法使用的功能,这是 和
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\ryany.SVCYINBOOK\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\ryany.SVCYINBOOK\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'
答案 0 :(得分:2)
您将能够打开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。”