python - 如何让wait_for等待大家的反应?
问题描述
embed = discord.Embed(title = "Currently Playing", colour = discord.Colour.blue())
embed.add_field(name = "Song", value = title, inline = False)
embed.add_field(name = "Length", value = str(datetime.timedelta(seconds = length)), inline = False)
embed.add_field(name = "Link", value = url, inline = False)
msg = await ctx.send(embed=embed)
await msg.add_reaction("\u23F8")
await msg.add_reaction("\u25B6")
await msg.add_reaction("\u23F9")
while True:
try:
reaction, user = await client.wait_for("reaction_add", check=lambda reaction, user: user.id == ctx.author.id and reaction.message.id == msg.id and reaction.emoji in ["\u23F8", "\u25B6", "\u23F9"], timeout = length)
except asyncio.TimeoutError:
return await msg.clear_reactions()
async def process():
if reaction.emoji == "\u23F8":
await msg.remove_reaction(reaction.emoji, ctx.author)
ctx.voice_client.pause()
elif reaction.emoji == "\u25B6":
await msg.remove_reaction(reaction.emoji, ctx.author)
ctx.voice_client.resume()
elif reaction.emoji == "\u23F9":
await msg.remove_reaction(reaction.emoji, ctx.author)
ctx.voice_client.stop()
asyncio.create_task(process())
在这里,我有一段来自播放命令的代码,它应该在请求歌曲后发送一条消息,该消息具有在用户做出反应时暂停、播放或停止的反应。此外,机器人会在反应被击中后立即自动删除反应,以便同一用户可以多次使用反应。
目前代码运行良好,唯一的问题是反应只响应最初调用播放命令的人,而不是服务器中的其他任何人。造成这种情况的原因很明显,这是由于user: user.id == ctx.author.id
wait_for中的 导致它只等待作者的反应,而await msg.remove_reaction(reaction.emoji, ctx.author)
这将导致它只删除作者添加的反应。当我尝试删除user: user.id == ctx.author.id
播放命令时完全停止工作。remove_reaction 需要一个成员参数,当我尝试用用户替换 ctx.author 时,user: user.id == ctx.author.id
它会做同样的事情,并且user: user.id == ctx.author.id
它没有任何效果,因为用户被定义为作者。任何帮助表示赞赏。
解决方案
推荐阅读
- django - 如何在管理面板中自定义多线程内联的“描述”
- python-3.x - 如何使用 while 循环修复此语法错误
- javascript - MomentJS 错误输出
- go - golang程序运行时是否可以重新加载动态库?
- php - PHP Soapclient 返回对象没有属性
- arrays - Python 3 输入:需要循环、拆分、字符串索引和 'r'、'w'、'r+' 的文本文件操作
- c# - c# - 在xmlDocument中选择节点
- python - 从质心点创建基于半径的分组
- r - 如何在 eventReactive 环境中显示向量的文本内容?
- node.js - 无法使用 Mongoose 和 mlab 推送 MongoDB 数组中的项目