Wallet command using a json file to store the data discord.py

Viewed 28

So im trying to make a little Economy Bot and im trying to make a commands for the user to gain coins. I copied a code I found on here and tweaked it a bit but it still doesn't work like I want it to.

This is the code I currently have

settings = json.load(open("testing.json", encoding="utf-8"))

@client.command()
async def beg(ctx):
    await account(ctx.author)
    user = ctx.author
    users = await data()
    earning = random.randint(0,101)
    await ctx.send(f"Someone gave you {earning}")

async def account(user):
    await data()
    users1 = settings["users"]
    if int(user.id) in users1:
        return False
    else:
        settings["users"].append(int(user.id))
        json.dump(settings, open("testing.json", "w", encoding="utf-8"), indent=4)
        settings["wallet"].append(0)
        json.dump(settings, open("testing.json", "w", encoding="utf-8"), indent=4)

async def data():
    with open("testing.json") as file:
        accounts = json.load(file)
    return accounts

I cant get it to make a Wallet just for just one user with there ID and then add a amount to his "wallet". I tried a few ways but nothing works and getting the value to display it in a Wallet command didnt work either. Im greatfull for any Help I get.

{
    "users": [
    ],
    "wallet": [
    ]
}

This is how the json looks like.

1 Answers

You could use the dictionary keys as the user ids, inside each store the data

users = {}
users[1] = 0
users[2] = 999

then if you want to access wallet of user 2

wallet = users[2]

Say you want to save a new user with 0 balance. And it's ID is 41

users[41] = 0

then to retrive that 0 just do

wallet = users[41]
Related