【发布时间】:2021-09-19 12:48:04
【问题描述】:
我正在制作一个不和谐的迷你游戏,其中机器人允许我们在掷骰上下注,我需要让它在其他人接受赌注之前不会运行,所以我尝试制作一个 while 循环以打印不接受,直到其他人输入 !accept 但它不起作用,而且由于某种原因,当一个人获胜时它不会更新字典,所以即使在死亡滚动之后,无论输赢,一个人的积分仍然保持 1000
代码:
@client.command()
async def deathroll(ctx,user:discord.User,bet=100):
roller1 = ctx.author.id
roller2 = user.id
accepted=False
if roller1 not in keys:
await ctx.send("make an account first")
if roller2 not in keys:
await ctx.send("please give a user for roller2")
while accepted!=True:
print("not accepted")
time.sleep(10)
if roller2 in keys:
roll=100
while roll!=1:
roll=random.randint(1,roll)
await ctx.send("roller1,rolled:"+str(roll))
if roll==1:
await ctx.send("the winner is roller1")
keys[roller1]=keys[roller1]-bet
keys[roller2]=keys[roller2]+bet
break
roll=random.randint(1,roll)
await ctx.send("roller2,rolled:"+str(roll))
if roll == 1:
await ctx.send("the winner is roller1")
keys[roller2]=keys[roller2]-bet
keys[roller1]=keys[roller1]+bet
break
@client.command()
async def accept(ctx):
accepted=True
【问题讨论】:
-
我怀疑它不起作用的原因是因为我认为它在尝试处理第二个命令之前等待第一个命令完成。这意味着 while 循环用一个它无法逃脱的循环阻塞了整个程序。您需要重写它以不阻塞程序,我建议将大部分逻辑移至接受函数,以便它仅在实际具有所有必要信息时尝试执行逻辑。
标签: python discord.py