Search code examples
pythondiscord.pycalculator

Making a fight command, but health doesnt subsctract


Goal: My goal is that you mention a user and it starts a battle, every 1 second a random amount between 5 and 20 health gets substracted from your current health. This happens until someone reaches 0 health.

My problem: In the upper code, the code sends a message every second, doesnt substract | In the bottom code the code doesnt send a message every second but it says (edited). Also the substraction doesnt work

@client.command()
async def fight(ctx, user : discord.Member = None):
  turn1 = 100
  turn2 = 100
  beginner = ctx.author

  embedprogress=discord.Embed(title="⚔️Fighting!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
  msg = await ctx.send(embed=embedprogress)

  while turn1 or turn2 > 0:
    turn1 - random.randint(5, 20)
    turn2 - random.randint(5, 20)
    await asyncio.sleep(1)
    await msg.edit(embed=embedprogress)
  else:
    embedover=discord.Embed(title="⚔️Fight ended!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
    await ctx.send(embed=embedover)
async def fight(ctx, user : discord.Member = None):
  turn1 = 100
  turn2 = 100
  beginner = ctx.author

  embedprogress=discord.Embed(title="⚔️Fighting!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
  msg = await ctx.send(embed=embedprogress)

  while turn1 or turn2 > 0:
    turn1 -= random.randint(5, 20)
    turn2 -= random.randint(5, 20)
    await asyncio.sleep(1)
    await msg.edit(embed=embedprogress)
  else:
    embedover=discord.Embed(title="⚔️Fight ended!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
    await ctx.send(embed=embedover)

The difference between the two codes is the equal sign in the turn1 - random.randint(5, 20) line.


Solution

  • you defined the embed but when the health changed you didn't update it, and there were I few other errors that I fixed, I can tell you don't have much python experience I suggest you learn the language before you try to use it. here is the fixed code:

    @client.command()
    async def fight(ctx, user : discord.Member = None):
        turn1 = 100
        turn2 = 100
        beginner = ctx.author
    
        embedprogress=discord.Embed(title="⚔️Fighting!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
        msg = await ctx.send(embed=embedprogress)
    
        while True:
            turn1 -= random.randint(5, 20)
            turn2 -= random.randint(5, 20)
            embedprogress=discord.Embed(title="⚔️Fighting!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
            await asyncio.sleep(1)
            await msg.edit(embed=embedprogress)
            if turn1 < 0 or turn2 < 0:
                break
    
        embedover=discord.Embed(title="⚔️Fight ended!", color=0x7289da, description=f"{beginner.mention} ❤️ - {turn1}\n{user.mention} ❤️ - {turn2}")
        await msg.edit(embed=embedover)