Reputation: 131
I'm making a discord bot and for the fight system I want the code to repeat the damage chunk if the monster isn't dead. I'm not sure how to do this but I know there's something you can do with a while true and break but I don't kno how to implement it. Here is the code:
@client.command(name='fight_01')
@commands.cooldown(1, 30, commands.BucketType.user)
async def fight_goblin(ctx):
rng1 = random.randint(1, 3)
users[str(user.id)]['goblin_hp_json'] = 5
rng2 = random.randint(1, 3)
if rng1 == int('2'):
await ctx.channel.send(
"You searched all around and couldn't find a goblin, try again next time :("
)
else:
await ctx.channel.send('You encountered a wild goblin!')
goblin_hp = int('5')
time.sleep(1.5)
rng3 = random.randint(1, 10)
if rng3 == int('3') or int('4') or int('7'):
users[str(user.id)]['goblin_hp_json'] = 5
user = ctx.author
users = await get_bank_data()
await ctx.channel.send(
'The goblin got the first hit and you lost 1hp')
users[str(user.id)]['hp'] -= int('1')
dmg = users[str(user.id)]['max_damage'
rng4 = random.randint(0, dmg)
rng5 = str(rng4)
time.sleep(1.5)
content1 = 'You did ' + rng5 + ' damage'
users[str(user.id)]['goblin_hp_json'] - int(rng4)
with open("mainbank.json", 'w') as f:
json.dump(users, f)
users = await get_bank_data()
await ctx.channel.send(content1)
if users[str(user.id)]['hp'] <= 0:
time.sleep(1.5)
await ctx.channel.send('You died, better luck next time')
if users[str(user.id)]['goblin_hp_json'] < int('1'):
content3 = 'You did ' + rng5 + ' damage so you killed the goblin, well done'
await ctx.channel.send(content3)
rng6 = random.randint(20, 100)
users[str(user.id)]['bank'] + rng6
time.sleep(0.8)
content4 = 'You gained ' + str(
rng6) + ' coins for defeating the goblin'
await ctx.channel.send(content4)
with open("mainbank.json", 'w') as f:
json.dump(users, f)
else:
(This is where I want it to return to the first else: statement)
Upvotes: 0
Views: 39
Reputation: 306
Firstly, before ansewring your question, I will give you some advice:
time.sleep
but await asyncio.sleep
(you have to import it before), you can see why here.int('5')
in python code, you can just use 5
. Python will automatically know that it's an integer.if rng3 == int('3') or int('4') or int('7'):
, you can use instead if rng3 in (3, 4, 7):
it's easier and clearer.Of course, except the first one (which is code breaker), you can continue to use your way of coding!
Now, I will respond to your question. The best way for me to do what you want to do is using while True
loop. Your code will repeat indefinitely but you are going exit it when you want. Here is your code :
@client.command(name='fight_01')
@commands.cooldown(1, 30, commands.BucketType.user)
async def fight_goblin(ctx):
rng1 = random.randint(1, 3)
users[str(user.id)]['goblin_hp_json'] = 5
rng2 = random.randint(1, 3)
if rng1 == int('2'):
await ctx.channel.send(
"You searched all around and couldn't find a goblin, try again next time :("
)
else:
await ctx.channel.send('You encountered a wild goblin!')
goblin_hp = int('5')
time.sleep(1.5) #use await asyncio.sleep(1.5) here rather
rng3 = random.randint(1, 10)
if rng3 == int('3') or int('4') or int('7'):
users[str(user.id)]['goblin_hp_json'] = 5
user = ctx.author
users = await get_bank_data()
await ctx.channel.send(
'The goblin got the first hit and you lost 1hp')
users[str(user.id)]['hp'] -= int('1')
dmg = users[str(user.id)]['max_damage'] #You forgot an ] here so I replaced it
rng4 = random.randint(0, dmg)
rng5 = str(rng4)
time.sleep(1.5) #use await asyncio.sleep(1.5) here rather
content1 = 'You did ' + rng5 + ' damage'
users[str(user.id)]['goblin_hp_json'] - int(rng4)
with open("mainbank.json", 'w') as f:
json.dump(users, f)
users = await get_bank_data()
await ctx.channel.send(content1)
while True: #an infinite loop
if users[str(user.id)]['hp'] <= 0:
time.sleep(1.5)
await ctx.channel.send('You died, better luck next time')
#If your command stop here you can use return, else use break to get out of the loop, here I will use break
break
if users[str(user.id)]['goblin_hp_json'] < int('1'):
content3 = 'You did ' + rng5 + ' damage so you killed the goblin, well done'
await ctx.channel.send(content3)
rng6 = random.randint(20, 100)
users[str(user.id)]['bank'] + rng6
time.sleep(0.8) #use await asyncio.sleep(0.8) here rather
content4 = 'You gained ' + str(
rng6) + ' coins for defeating the goblin'
await ctx.channel.send(content4)
with open("mainbank.json", 'w') as f:
json.dump(users, f)
#Same here, I will use break
break
else:
(This is where I want it to return to the first else: statement)
#Here you can use continue to repeat your loop, or just delete this part (an you will return to the first if). To match with your current code, I will use continue
continue
Your code will repeat itself indefinitely unless you enter one of the first 2 conditions.
Hope my solution will work for you!
Have a nice day
Upvotes: 1