python - 当我等待“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
.
我试过了:
- 阅读文档,并且 function
wait_for()
的文档指出,“event (str) – 事件名称,类似于事件引用,但没有 on_ 前缀,等待。”,并且事件引用表明正确的术语确实是“reaction_remove”而不是别的 - 检查错别字
- 确保 Bot.Intent.reactions == True
- 确保我有最新版本的 discord.py 模块
- 如上所述使用断点进行调试。
- 考虑到我的理智,毕竟这个问题只是我错过的一些愚蠢的错字。
- 确保我的机器人在不和谐的角色中拥有它所需的所有权限
解决方案
我认为wait_for
只能处理“消息”和“reaction_add”,不能处理“reaction_remove”。
推荐阅读
- python - 带有 cp1252 编码字符串的 python3 C 扩展模块
- android - 同时打开相机并设置手电筒模式
- koa - 本地运行 Koa 服务器时请求 IP 地址为 ::1
- python - ImportError 无法导入名称 celery
- java - CMD(Java)中的Postgres恢复不再响应,而相同的命令在CMD中执行良好
- java - 在hibernate中加入不相关的实体(5.1之后)
- mysql - 设计我的 mysql 表的最佳方法是什么?我需要用偏好图代表用户
- .net-core - 与 log4net 相比,使用 Nlog 的性能较慢
- c# - 尝试实施软删除时出现映射错误
- c# - XML 标记名称包括父类 ASP.NET