简体   繁体   中英

Problem with if and else (python, discord.py)

recently I started making discord bots using python. I made some commands like checking how much user1 loves user2 . user1/2 cannot be the bot itself or the same person. It worked fine most of the time, but when I added other command like this everything stops working. Here's my code:

@bot.command()
@commands.check(user_is_me)
async def milosc(ctx, user1: discord.Member, user2: discord.Member):
    milosc_procent = random.randint(1, 101)
    if user1 == user2:
        await ctx.send("**Nie możesz sprawdzic jak bardzo ktoś kocha samego siebie**")
    if user1.name == 'Pieseł bot':
        await ctx.send("**Nie możesz sprawdzić jak bardzo bot kogoś kocha. To nie ma sensu !**")
    if user2.name == 'Pieseł bot':
        embed=discord.Embed(title="Jak bardzo kochasz Pieseł bot-a", description=f"Użytkownik {user1.name} kocha użytkownika {user2.name} w 100%. Bo kto takiego słodkiego piesełka by nie kochał <:pepeLove:831157130153164831>", color=0xff0000, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="Pieseł bot", icon_url="https://cdn.discordapp.com/avatars/451734929999659008/ed54c994c64b2e0df385822e42c48993.png?size=4096")
        embed.set_footer(text=f"Sprawdzono na prośbę {ctx.author.name}")
        await ctx.send(embed=embed)
    else:
        embed=discord.Embed(title="Jak bardzo się kochają", description=f"Użytkownik {user1.name} kocha użytkownika {user2.name} w {milosc_procent}%", color=0xff0000, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="Pieseł bot", icon_url="https://cdn.discordapp.com/avatars/451734929999659008/ed54c994c64b2e0df385822e42c48993.png?size=4096")
        embed.set_footer(text=f"Sprawdzono na prośbę {ctx.author.name}")
        await ctx.send(embed=embed)
        if milosc_procent >= 100:
            await ctx.send(file=discord.File('milosc.png'))
        
@bot.command()
@commands.check(user_is_me)
async def para(ctx, user1: discord.Member, user2: discord.Member):
    how_para = random.randint(1, 101)
    if user1 == user2:
        await ctx.send("**Nie możesz sprawdzic czy ktoś jest parą z samym sobą :exploding_head:**")
    if user1.name == 'Pieseł bot':
        await ctx.send("**Nie możesz sprawdzić jak bardzo bot pasuje do bycia z kimś parą. To nie ma sensu !**")
    if user2.name == 'Pieseł bot':
        await ctx.send("**Nie możesz sprawdzić jak bardzo bot pasuje do bycia z kimś parą. To nie ma sensu !**")
    else:
        embed=discord.Embed(title="Jak bardzo pasują jako para", description=f"Użytkownik {user1.name} z użytkownikiem {user2.name} byli by zarąbistą parą w {how_para}%", color=0xff0000, timestamp=datetime.datetime.utcnow())
        embed.set_author(name="Pieseł bot", icon_url="https://cdn.discordapp.com/avatars/451734929999659008/ed54c994c64b2e0df385822e42c48993.png?size=4096")
        embed.set_footer(text=f"Sprawdzono na prośbę {ctx.author.name}")
        await ctx.send(embed=embed)
        if how_para >= 100:
            await ctx.send(file=discord.File('para.gif'))

It should look like this:它应该是什么样子

But it's look like this:它看起来如何

Bot is for Polish users, that's why all messages are in polish.

If you want to make 1 loop with multiple 'if' statements you should use 'elif'. 'Elif' is short for else if. If the condition for the if is False , the code will check the condition elif . If all the conditions if and elif are false the code will execute the else block. See the example below.

user1 = 'mike'
user2 = 'paul'

if user1 == user2:
    print('User 1 is equal to user 2')
elif user1 == 'paul':
    print('User 1 is Paul')
else:
    print('No users recognized')

Your code will now execute the else block in the last if - else statement each time the last if condition is not satisfied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM