简体   繁体   中英

Nesting If and Else statements for Discord.Py

I've been struggling with this command for a while. I can get one check to work, but not the other. I want to make sure the user can't mention themselves AND that they can't "rob" another user if they have less than 0 dollars being pulled from get_dollars. I've tried "try, except, else, finally" and got it to semi-work but it would spit out two messages instead of one. I then tried the code below thinking if I nested if and else statements that it would work better. I get an if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment error trying it this way.

if message.content.startswith('!rob'):
        if message.author == user.mention:
            await client.send_message(message.channel, "{} you cant rob yourself! 👊".format(message.author.mention))
        else:
            if get_dollars(message.author) < 0:
                await client.send_message(message.channel, "{} you can't even afford a gun.".format(message.author.mention))
            else:
                for user in message.mentions:
                    if (get_dollars(user)) < 1:
                        await client.send_message(message.channel, "{} is too broke to rob 🤣".format(user.mention))
                    elif steal <= 3:
                        print("{} catches {} slippin and sticks them up for ${} 😨🔫".format(message.author.mention, user.mention, stealdollars))
                        await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨🔫".format(message.author.mention, user.mention, stealdollars))
                        remove_dollars(user, stealdollars)
                        add_dollars(message.author, stealdollars)
                    elif steal == 4:
                        print("{} gets arrested trying to rob {} and has to post $250 bail 👮🚨".format(message.author.mention, user.mention))
                        await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post $250 bail 👮🚨".format(message.author.mention, user.mention))
                        remove_dollars(message.author, 250)
                    elif steal >= 5:
                        print("{} kicks {}'s door down but doesn't find any cash 🤬".format(message.author.mention, user.mention))
                        await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash 🤬".format(message.author.mention, user.mention))

Like Patrick said, theres no definition of user. Ended up putting user = message.mentions[0] and the command works fine.

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