Search code examples
pythonjsonasynchronousdiscorddiscord.py

Discord.py- Economy system


My code:

@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)

Error:

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

So I was making an economy system for my discord bot, and faced this error. I dont really know how to fix this, and doing stuff only leads to more errors. It would be cool if someone could correct, and probably explain where i went wrong.


Solution

  • Your provided code makes no sense at all in some places, for example:

    users = json.dump(users,f)
    

    users has no further use and no effect on the code.

    Furthermore, your beg command contains a logical error. You try to read a JSON (r) and dump at the same time, then you get the error that you cannot write to the JSON file.

    To get around all this we come to the following code:

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