Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

discord.py - Trying to make a kind of pls dep all or pls dep max kind of thing

I'm making a currency bot but I can't seem to figure out how to do something like: [prefix] deposit all/[prefix] withdraw all or [prefix] deposit max/[prefix] withdraw max but I can't seem to figure out how to do it. This is my code so far for deposit:

@client.command()
async def dep(ctx, amount=0):
    users = await get_bank_data()
    user = ctx.author
    if amount > 0:
      users[str(user.id)]['wallet'] -= amount
      users[str(user.id)]['bank'] += amount

      with open("mainbank.json", "w") as f:
        json.dump(users, f)

      await ctx.send(f"You have deposited {amount} coins!")

    elif amount < 0:
      await ctx.send(f"You can't deposit a negative amount of coins stupid!")

    elif amount == 0:
      await ctx.send(f"You can't deposit 0 coins stupid!")```
question from:https://stackoverflow.com/questions/65617624/trying-to-make-a-kind-of-pls-dep-all-or-pls-dep-max-kind-of-thing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can simply use the in keyword to check if the amount arg is either max or all, to check if it's a digit use str.isdigit()

async def dep(ctx, amount: str):
    if amount.lower() in ["max", "all"]:
        # Deposit all the money here

    elif amount.isdigit() and int(amount) > 0:
        amount = int(amount) 
        # The `amount` variable is now a positive integer, deposit it here

You can do the withdraw command on the same basis


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...