机器人如何在不复制或循环代码的情况下一遍又一遍地说出消息
How can the bot says the message over and over without the code being duplicated or looped
所以我正在尝试发出战斗命令,但我不知道如何在不复制 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
然后重复回合直到其中一名玩家被击败。
您可以为此使用 while 循环。将您的条件放入 return 或在条件匹配时将其打破。下面的代码会让你知道如何去做。
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
一个简单的方法是:
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)
所以我正在尝试发出战斗命令,但我不知道如何在不复制 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
然后重复回合直到其中一名玩家被击败。
您可以为此使用 while 循环。将您的条件放入 return 或在条件匹配时将其打破。下面的代码会让你知道如何去做。
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
一个简单的方法是:
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)