首页 > 解决方案 > 当我等待“reaction_remove”时,Bot.wait_for() 不能正常工作

问题描述

预期的输出和我的代码是什么:

我的机器人应该发送一条消息,然后检查是否有人用 :tada: 对该消息作出反应,如果有人这样做了,它应该给那个用户一个特定的角色,这部分工作正常,但我也希望它检查用户是否删除了他们的反应,如果是,则删除角色。我把角色移除器和角色添加器放到各自的async协程函数中,

        # Listening for reactions
        await participation_message.add_reaction("")
        reaction_check = lambda reaction, user: str(reaction.emoji) == "" and reaction.message.id == participation_message.id # In case you might be wondering, participation_message is a discord.Message that I send before this code block

        async def remove_participants_loop():
            while True:
                try:
                    reaction, user = await self.client.wait_for('reaction_remove', timeout=60, check=reaction_check)
                    try:
                        await user.remove_roles(participant_role)
                    except Exception as e:
                        console_log("Error in removing participant role from user: {}".format(e), "white", "on_red")
                except TimeoutError:
                    break

        async def add_participants_loop(timeout=delete_after*60):
            while True:
                try:
                    reaction, user = await self.client.wait_for('reaction_add', timeout=60, check=reaction_check)
                    try:
                        await user.add_roles(participant_role)
                    except Exception as e:
                        console_log("Error in adding participant role to user: {}".format(e), "white", "on_red")
                except TimeoutError:
                    break

我把它们放到它们自己的协程中,因为我需要它们都异步运行,为此我现在这样做

        asyncio.create_task(add_participants_loop())
        asyncio.create_task(remove_participants_loop())

问题:

这适用于 ,add_participants_loop()但不适用于remove_participants_loop(),我尝试使用断点对其进行调试,发现remove_participants_loop确实运行正常,但是当它等待时"reaction_remove",当我删除我的反应时它没有检测到它,并继续等待并最终提高了asyncio.TimoutError.

我试过了:

标签: pythondiscordpython-asynciodiscord.py-rewrite

解决方案


我认为wait_for只能处理“消息”和“reaction_add”,不能处理“reaction_remove”。


推荐阅读