Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...