0

I'm fairly new to python and I have created a currency bot. The only issue is that it will not save the information to the .json file that I have created. I've even coded it to store the info there but I don't understand why it won't. The .json file is just { }, and when I run the bot nothing gets added there. When I do the ?beg command it will give a random number that should be added to the .json file, but it isn't.

client = commands.Bot(command_prefix="?")

os.chdir('/home/runner/spamtonbot/')

@client.event
async def on_ready():
    print("Ready")


@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.red())
    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)

    users = await get_bank_data()
    user = ctx.author
    earnings = random.randrange(101)

    await ctx.send("Someone gave you {} kromer!".format(str(earnings)))

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

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


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


@client.command()
async def get_bank_data():
    with open("kromerbank.json") as f:
        users = json.load(f)

        return users

Someone help. Thanks!

CCD
  • 470
  • 1
  • 7
  • 19
Ryan K
  • 1

1 Answers1

0

You aren't using the correct open mode.

Try changing

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

to

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

See: How do I write JSON data to a file?

CCD
  • 470
  • 1
  • 7
  • 19
  • with open("kromerbank.json", "w") as f: json.dump(users, f) ok i changed it to this but i still get an error message? – Ryan K Oct 22 '21 at 17:14
  • What is the error message? It is hard to debug it without that information. – CCD Oct 22 '21 at 17:19
  • Ignoring command beg: Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "main.py", line 36, in beg await open_account(ctx.author) File "main.py", line 52, in open_account users = await get_bank_data() File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 374, in __call__ return await self.callback(*args, **kwargs) File "main.py", line 69, in get_bank_data users = json.load(f) – Ryan K Oct 22 '21 at 17:29