僵尸程序如何一遍又一遍地说出消息而又没有重复或循环代码

时间:2020-07-19 13:06:10

标签: discord discord.py discord.py-rewrite

所以我试图发出一个战斗命令,但是我不知道如何在不复制python文件中代码的情况下使机器人重新发送消息。

  await ctx.send("Player 1, what will you do now? OPTIONS: Fight, Defense, Items, Surrender")
  def check(m):
    return m.channel == ctx.channel and m.author == ctx.author
  send = await client.wait_for('message', check=check)
  
  await ctx.send("Player 2, what will you do now? OPTIONS: Fight, Defense, Items, Surrender")
  def check(m):
    return m.channel == ctx.channel and m.author == ctx.author
  send = await client.wait_for('message', check=check)

我希望结果是这样的:

-- TURN 1 --
Bot: Player 1, what will you do now? OPTIONS: Fight, Defense, Items, Surrender
Player 1: Fight
Bot: Player 2, what will you do now? OPTIONS: Fight, Defense, Items, Surrender
Player 2: Defense
-- TURN 2 --
Bot: Player 1, what will you do now? OPTIONS: Fight, Defense, Items, Surrender
Player 1: Defense
Bot: Player 2, what will you do now? OPTIONS: Fight, Defense, Items, Surrender
Player 2: Fight

重复转弯,直到其中一位玩家被击败。

2 个答案:

答案 0 :(得分:1)

一个简单的方法是:

aysnc def action(ctx, player):
    await ctx.send(f"{player}, what will you do now? OPTIONS: Fight, Defense, Items, Surrender")
    def check(m):
        return m.channel == ctx.channel and m.author == ctx.author
    response = await client.wait_for('message', check=check)
    if "defense" in response.content.lower():
        #Do stuff
    elif "fight" in response.content.lower():
        #Do stuff
    elif "Items" in response.content.lower():
        #Do stuff
    elif "Surender" in response.content.lower():
        #Do stuff
    elif "end" in response.content.lower():
        return False
    return True

async def turn(ctx, nb):
    await ctx.send("-- Turn {nb} --")
    for player in ["player 1", "player 2"]:
        action = await action(ctx, player)
    await ctx.send("Game ended") if not action else await turn(ctx, nb+1)

答案 1 :(得分:0)

您可以为此使用while循环。将您的条件放入内部,一旦条件匹配,将其返回或破坏。下面的代码将使您了解如何执行此操作。

def check(m):
    return m.channel == ctx.channel and m.author == ctx.author
while True:
    await ctx.send("Player 1, what will you do now? OPTIONS: Fight, Defense, Items, Surrender")
    msg = await client.wait_for('message', check=check)
    if msg.content == 'Fight':
        pass
    elif msg.content == 'Defense':
        pass
    elif msg.content == 'Items':
        pass
    elif msg.content == 'Surrender':
        pass

    # You can end the Loop Once the Condition Matches, Example:
    if health >= 0:
        return