不和谐经济机器人 python 撤回命令不起作用有人知道为什么吗?

时间:2021-02-25 17:27:58

标签: python discord discord.py

所以我正在用 python 制作一个用于 discord 的机器人,它是一个经济机器人,它将所有数据存储在一个名为“MainBank.json”的 json 文件中,一切正常,除了当您尝试存款时发生一件事您存入银行的钱或当您试图从银行取款时。我看不出这有什么问题。

@bot.command()
async def withdraw(ctx,amount = None):
    await open_account(ctx.author)

    if amount == None:
        await ctx.send("Please enter a valid amount!")
        return
  
    bal = await update_bank(ctx.author)

    amount = int(amount)
    if amount>bal[1]:
        await ctx.send("You don't have enough potatoes in your bank!")
        return
    if amount<0:
        await ctx.send("Please enter a valid amount!")
        return

        await update_bank(ctx.author,amount, "wallet")
        await update_bank(ctx.author,-1*amount, "bank")

        await ctx.send(f"You withdrew {amount} potatoes!")

@bot.command()
async def deposit(ctx,amount = None):
    await open_account(ctx.author)

    if amount == None:
        await ctx.send("Please enter a valid amount!")
        return
  
    bal = await update_bank(ctx.author)

    amount = int(amount)
    if amount>bal[0]:
        await ctx.send("You don't have enough potatoes in your wallet!")
        return
    if amount<0:
        await ctx.send("Please enter a valid amount!")
        return

        await update_bank(ctx.author, -1*amount)
        await update_bank(ctx.author, amount, "bank")

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

async def open_account(user):

  users = await get_bank_data()

  if str(user.id) in users:
      return False
  else:
      users[str(user.id)] = {}
      users[str(user.id)]["wallet"] = 0
      users[str(user.id)]["bank"] = 0

  with open("MainBank.json", "w") as f:
      json.dump(users,f)
  return True

async def get_bank_data():
    with open("MainBank.json", "r") as f:
      users = json.load(f)

    return users



async def update_bank(user,change = 0,mode = "wallet"):
  users = await get_bank_data()

  users[str(user.id)][mode] += change

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

  bal = [users[str(user.id)]["wallet"],users[str(user.id)]["bank"]]
  return bal

1 个答案:

答案 0 :(得分:0)

你绝对要注意正确的缩进。 我注意到 withdraw 命令出现以下错误:

    if amount<0:
        await ctx.send("Please enter a valid amount!")
        return

        await update_bank(ctx.author,amount, "wallet")
        await update_bank(ctx.author,-1*amount, "bank")

        await ctx.send(f"You withdrew {amount} potatoes!")

您的 await update_bankawait ctx.send 参数必须在 if amount < 0: 语句下:

    if amount < 0:
        await ctx.send("**The amount must be positive.**")
        return
    await update_bank(ctx.author, amount)
    await update_bank(ctx.author, -1 * amount, "bank")

    await ctx.send(f"**You added {number_with_commas(amount)} coins to your wallet.**")

如果您不await 函数正确,它将无法工作。对于您没有正确缩进代码的 deposit 命令也是如此。