首页 > 解决方案 > 如何让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.idwait_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它没有任何效果,因为用户被定义为作者。任何帮助表示赞赏。

标签: pythondiscorddiscord.py

解决方案


推荐阅读