繁体   English   中英

为Discord.Py嵌套If和Else语句

[英]Nesting If and Else statements for Discord.Py

我已经为此命令苦苦挣扎了一段时间。 我可以拿一张支票上班,但不能拿另一张。 我想确保用户不会提及自己,并且如果他们从get_dollars提取的钱少于0美元,那么他们就不能“抢劫”另一个用户。 我尝试过"try, except, else, finally"并使其半成品,但它会吐出两条消息,而不是一条消息。 然后,我尝试下面的代码,以考虑是否嵌套ifelse语句,它将更好地工作。 我得到一个if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment错误以这种方式尝试if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment

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))

就像Patrick所说的那样,没有用户的定义。 最终将user = message.mentions [0]放进去,命令运行正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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