问题描述
我想使用异步检查功能:
async def check(reaction,user):
await x
return y
await self.bot.wait_for('reaction_add',check=check)
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ExtensionFailed: Extension 'cogs.boost' raised an error: SyntaxError: 'await' outside async function
如果我将代码更改为:
async def check(reaction,user):
await x
return y
await self.bot.wait_for('reaction_add',check=await check)
我收到以下错误消息:
TypeError: object function can't be used in 'await' expression
是否可以使用异步检查功能?谢谢!
解决方法
async def predicate(ctx):
# you can use any async code here
result = await stuff()
return result
return commands.check(predicate)
只需从中添加异步即可使用等待功能。
当您调用该函数时,请在前面使用Await
r =等待谓词()
,您可以使用以下代码作为示例来编写异步检查:
{{1}}
更多信息可以在这里找到:discord.py docs