Discord 机器人怎么能一遍又一遍地说消息而不重复或循环代码呢
因此,我试图发出一个作战命令,但我不知道如何让机器人在不复制python文件中的代码的情况下重新发送消息Discord 机器人怎么能一遍又一遍地说消息而不重复或循环代码呢,discord,discord.py,discord.py-rewrite,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
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循环。把你的条件放在里面,一旦条件匹配就返回或打破它。下面的代码将告诉您如何做
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动作(ctx,播放器):
等待ctx.send(f“{player},你现在将做什么?选项:战斗、防御、物品、投降”)
def检查(m):
返回m.channel==ctx.channel和m.author==ctx.author
响应=等待客户端。等待('消息',检查=检查)
如果response.content.lower()中出现“防御”:
#做事
elif在response.content.lower()中“fight”:
#做事
elif response.content.lower()中的“Items”:
#做事
elif response.content.lower()中的“Surender”:
#做事
elif response.content.lower()中的“end”:
返回错误
返回真值
异步def转向(ctx,nb):
等待ctx.send(“--Turn{nb}--”)
对于[“玩家1”、“玩家2”]中的玩家:
动作=等待动作(ctx,玩家)
等待ctx。发送(“游戏结束”),如果没有行动,则等待回合(ctx,nb+1)