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!