【问题标题】:discord.py mini-game commanddiscord.py 小游戏命令
【发布时间】:2021-05-25 08:36:05
【问题描述】:

我正在尝试发出一个命令,您可以在其中捕捉凤凰,并随机对您和会员进行 ping 操作,如果您输入 catch,它会在您的计数器上加一分。但是我试过了,最远的是它让我们输入catch。当我输入catch 时它没有响应。

@client.command(aliases=["ctp", "capturethephoenix"])
async def catchthephoenix(ctx, member : discord.Member=None):
  points = {ctx.author: 0, member: 0}
  random_time = random.randrange(30)
  if member == None:
    await ctx.send(f"{ctx.author.mention}, You need to mention a member to play with.")
  if member == client.user:
    await ctx.send(f"{ctx.author.mention}, Hey! Are you trying to catch me??! Mention someone else.")
  if member.bot == True:
    await ctx.send(f"{ctx.author.mention}, You can't play with a bot.")
  else:
    game = True
    await ctx.send(f"{ctx.author.mention} and {member.mention}, I will alert you when a phoenix is found so you can catch it.")
    def check(m):
      return m.author.id == ctx.author.id
    while True:
      try:
        message = await client.wait_for('message', check=check, timeout=45)
        await asyncio.sleep(random_time)
        await ctx.send(f"{ctx.author.mention} and {member.mention}, A Phoenix has been found! Type `catch` to catch it!")
        if message.author.id == member.id and message.content.lower() == "catch":
          points[member] += 1
          await ctx.send(f"{member.name} has caught the phoenix! They have **{points[member]}** point(s)!")
        elif message.author.id == ctx.author.id and message.content.lower() == "catch":
          points[ctx.author] += 1
          await ctx.send(f"{ctx.author.name} has caught the phoenix! They have **{points[ctx.author]}** point(s)!")
      except:
        game = False
        embed = discord.Embed(
          color = embed_color,
          title = "Game Over",
          description = "No one caught the Phoenix in time so the game is over. Final Scores Below.")
        embed.add_field(name = f"{member.name}'s score", value = f"{points[member]}")
        embed.add_field(name = f"{ctx.author.name}'s score", value = f"{points[ctx.author]}")
        await ctx.send(embed=embed)

【问题讨论】:

  • 请澄清您所说的“它没有响应”是什么意思。有错误吗?您是否尝试过任何调试来确定代码无法按预期运行的确切位置?
  • 当我输入 catch 时,机器人没有响应,也没有错误。这是发生的事情的图片。 imgur.com/a/5U5Pe8a

标签: python python-3.x discord.py


【解决方案1】:

这是因为您在 await asyncio.sleep()await ctx.send() 语句(在 try 子句内)之前调用了 client.wait_for()client.wait_for() 调用应该在之后

@client.command(aliases=["ctp", "capturethephoenix"])
async def catchthephoenix(ctx, member: discord.Member=None):
    points = {ctx.author: 0, member: 0}
    random_time = random.randrange(30)

    game = False
    if member is None:
        await ctx.send("...")
    elif member == client.user:
        await ctx.send("...")
    elif member.bot:
        await ctx.send("...")
    else:
        game = True

    await ctx.send(...)
    while game:
        try:
            await asyncio.sleep(random_time)
            await ctx.send(...)
            message = await client.wait_for(
                "message",
                check=lambda m: m.author.id == ctx.author.id,
                timout=45.0
            )
        except asyncio.TimeoutError:
            game = False
            ...
        if not message.content.lower() == "catch":
            continue
        if message.author.id == member.id:
            ...
        elif message.author.id == ctx.author.id:
            ...

【讨论】:

  • 谢谢你成功了。最后一件事,如何让它检查游戏是否已经在运行?
  • @Phoenix 你可以写成while game,而不是while True。然后,您可以将其实现为命令或其他情况,以检查 message 的内容(例如,if message.content.lower() == "running":)。
猜你喜欢
  • 2021-10-06
  • 2021-05-22
  • 1970-01-01
  • 2021-07-25
  • 2021-08-01
  • 2012-02-09
  • 2021-03-26
  • 2021-08-28
  • 2021-01-22
相关资源
最近更新 更多