简体   繁体   English

有没有办法对discord.py wait_for使用异步检查功能?

[英]Is there a way to use an async check function for discord.py wait_for?

I want a method to change我想要一种改变的方法

async def check(reaction, user):
   await x
   return y

await self.bot.wait_for('reaction_add', check=check)

This yields an error message that says the check function isn't awaited:这会产生一条错误消息,指出不等待检查函数:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ExtensionFailed: Extension 'cogs.boost' raised an error: SyntaxError: 'await' outside async function 

If I change the code to:如果我将代码更改为:

async def check(reaction, user):
   await x
   return y
await self.bot.wait_for('reaction_add', check=await check)

I get the following error message:我收到以下错误消息:

TypeError: object function can't be used in 'await' expression

Is there any way to have an async check function?有什么办法可以有异步检查功能吗? Thanks!the current help command to acustom command, in my bot using discord.py谢谢!当前帮助命令到自定义命令,在我的机器人中使用 discord.py

From delving into the code (on discord.py 1.5.1), there does not appear to be a way to use a coroutine as your check function.从深入研究代码(在 discord.py 1.5.1 上),似乎没有办法使用协程作为检查函数。 There might be a hacky workaround that exists, but it would not be best practice and would lead to a lot more problems than you would solve by implementing it.可能存在一种 hacky 解决方法,但这不是最佳实践,并且会导致比您通过实施它解决的问题更多的问题。 If you want to utilize check , just write a synchronous version of the function that executes the check, and make sure to make it as efficient as possible to keep from blocking.如果您想使用check ,只需编写执行检查的函数的同步版本,并确保使其尽可能高效以避免阻塞。

Try this:尝试这个:

import asyncio
@...
async def your_command(ctx, ...):

    async def run():
        # you can use await there
        return ...


    def check(msg):
        result = asyncio.create_task(run()) # run the async function
        return resul

    ctx = client.wait_for("message", check=check)

and there is an example:有一个例子:

import asyncio

@client.command()
async def test(ctx):

    async def run(msg):
        await msg.send("This message is not yours!", hidden=True)
        return


    def check(msg):

        if msg.author.id == ctx.author.id:
            return True
        else:
            asyncio.create_task(run(msg))
            return False

    ctx = await client.wait_for("raw_reaction_add", check=check)

    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法对 discord.py wait_for() 使用异步检查 function? - Is there a way to use an async check function for discord.py wait_for()? 在 discord.py wait_for 上使用 check() 出现类型错误 - TypeError with check() on discord.py wait_for 如何在 discord.py 中使用 wait_for function? - How to use wait_for function in discord.py? 如何以动态方式使用与“wait_for”一起使用的 Discord.py“检查”参数/函数? 我想将参数传递给“支票” function - How can I use the Discord.py 'check' parameter/function used with 'wait_for' in a dynamic way? I want to pass params to the 'check' function 检查谁对 wait_for 中的消息做出反应检查 discord.py - check who reacted to a message in a wait_for check discord.py wait_for 检查不起作用 discord.py - wait_for check does not works discord.py discord.py wait_for 检查工作不正常 - discord.py wait_for check isnt working properly 如何在discord.py上的wait_for中使用多个检查 - How to use more than one check in a wait_for on discord.py (discord.py) wait_for 函数不检查作者是否对消息做出反应 - (discord.py) wait_for function not checking if author reacted to message 有没有办法在 discord.py 的“on_raw_reaction_add”中使用“wait_for 消息” - Is there a way to use “wait_for message” in “on_raw_reaction_add” in discord.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM