如何获取Discord消息中被移除的表情反应响应?
捕获用户移除表情的反应值
要获取用户移除表情时的表情值,只需将监听的事件从reaction_add改为**reaction_remove**,用法和原代码完全一致:
reaction, user = await self.bot.wait_for( 'reaction_remove', check = lambda reaction, user: reaction.message == message and str(reaction.emoji) in ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '◀️', '▶️', '❌'], timeout = 60 ) # 被移除的表情可通过 reaction.emoji 获取 print(f"用户{user}移除了表情:{reaction.emoji}")
补充说明
reaction_remove事件触发时,返回的reaction对象同样包含被移除的表情信息,user是执行移除操作的用户- 如果需要同时监听表情添加和移除操作,可以用
asyncio.gather同时等待两个事件:
import asyncio # 定义共用的检查函数 def reaction_check(reaction, user): return reaction.message == message and str(reaction.emoji) in ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '◀️', '▶️', '❌'] # 同时等待添加和移除事件 task_add = self.bot.wait_for('reaction_add', check=reaction_check, timeout=60) task_remove = self.bot.wait_for('reaction_remove', check=reaction_check, timeout=60) done, pending = await asyncio.wait([task_add, task_remove], return_when=asyncio.FIRST_COMPLETED) for task in done: reaction, user = task.result() if task is task_add: print(f"用户{user}添加了表情:{reaction.emoji}") else: print(f"用户{user}移除了表情:{reaction.emoji}")
内容的提问来源于stack exchange,提问作者Kim
相关产品推荐
相关产品推荐

