首页 > 解决方案 > Discord.py-经济系统

问题描述

我的代码:

@client.command()
async def balance(ctx):
  await open_account(ctx.author)

  user = ctx.author

  users = await get_bank_data()
  
  wallet_amt= users[str(user.id)]["Wallet"]
  bank_amt= users[str(user.id)]["Bank"]


  em = discord.Embed(title=f"{ctx.author.name}'s balance.", color=discord.Color.teal()) 
  em.add_field(
    name="Wallet Balance",value=wallet_amt
  )
  em.add_field(
    name="Bank Balance",value=bank_amt
  )
  await ctx.send(embed=em)

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("bank.json",'w') as f:
    users = json.dump(users,f)
  return True

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

@client.command()
async def beg(ctx):
  await open_account(ctx.author)

  user = ctx.author


  users = await get_bank_data()

  earnings = random.randrange(101)
  await ctx.send(f"Someone gave your {earnings} coins")

  users[str(user.id)]["Wallet"] += earnings

  with open("bank.json",'r') as f:
    users = json.dump(users,f)

错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

所以我正在为我的不和谐机器人制作一个经济系统,并遇到了这个错误。我真的不知道如何解决这个问题,做事只会导致更多错误。如果有人可以纠正会很酷,并且可能会解释我哪里出错了。

标签: pythonjsonasynchronousdiscorddiscord.py

解决方案


它只是 { } 写它它之所以起作用是因为 json 将文件存储为字典,并且还在 beg 命令中使用这一行与 open("bank.json", 'r') as f: users = json.dump(users, f)将 R(读取模式)更改为 W(写入模式),这样可以存储您拥有的硬币数量


推荐阅读