[英]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.
我想确保用户不会提及自己,并且如果他们从get_dollars提取的钱少于0美元,那么他们就不能“抢劫”另一个用户。 I've tried
"try, except, else, finally"
and got it to semi-work but it would spit out two messages instead of one. 我尝试过
"try, except, else, finally"
并使其半成品,但它会吐出两条消息,而不是一条消息。 I then tried the code below thinking if I nested if
and else
statements that it would work better. 然后,我尝试下面的代码,以考虑是否嵌套
if
和else
语句,它将更好地工作。 I get an if message.author == user.mention: UnboundLocalError: local variable 'user' referenced before assignment
error trying it this way. 我得到一个
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))
Like Patrick said, theres no definition of user. 就像Patrick所说的那样,没有用户的定义。 Ended up putting user = message.mentions[0] and the command works fine.
最终将user = message.mentions [0]放进去,命令运行正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.