'How do I retrieve data from my JSON file uploaded on Heroku (discord.py) [duplicate]
I am only using JSON file for database in discord.py.
The problem is, when I am deploying new update to my bot, my old JSON data is overwritten with the one on my computer, so any data changes done in my bot is deleted and replaced with the one saved on my computer.
I tried heroku git:clone -a (app-name), but the data on it was the one before I deployed it.
How can I retrieve the latest date of my JSON file? So whenever I deployed a new update, the data of my JSON is still the same.
My main.py
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)]["name"] = "NULL"
users[str(user.id)]["Wallet"] = 0
users[str(user.id)]["Bank"] = 0
users[str(user.id)]["data1"] = 0
users[str(user.id)]["data2"] = 0
users[str(user.id)]["data3"] = 0
users[str(user.id)]["data4"] = 0
users[str(user.id)]["data5"] = 0
users[str(user.id)]["data6"] = 0
users[str(user.id)]["data7"] = 0
users[str(user.id)]["data8"] = 0
users[str(user.id)]["data9"] = 0
users[str(user.id)]["data10"] = 0
users[str(user.id)]["data11"] = 0
users[str(user.id)]["data12"] = 0
users[str(user.id)]["data13"] = 0
users[str(user.id)]["data15"] = 0
users[str(user.id)]["data16"] = 0
users[str(user.id)]["data17"] = 0
users[str(user.id)]["data18"] = 0
users[str(user.id)]["data19"] = 0
users[str(user.id)]["data20"] = 0
users[str(user.id)]["data21"] = 0
users[str(user.id)]["data22"] = 0
users[str(user.id)]["data23"] = 0
with open("quantum.json", 'w') as f:
json.dump(users, f)
return True
async def get_bank_data():
with open("quantum.json", 'r') as f:
users = json.load(f)
return users
@bot.command()
async def bal(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)
@bot.command()
async def beg(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
income = random.randrange(101)
users[str(user.id)]["name"] = str(ctx.author.name)
await ctx.send(f"Someone gave your {income} coins")
print(f"{ctx.author} Received '{income}' coins | UserID: {ctx.author.id}")
users[str(user.id)]["Wallet"] += income
with open("quantum.json", 'w') as f:
json.dump(users, f, indent=4)
Solution 1:[1]
Basically, this is not how you save data in Heroku. Heroku instances are restarted once a day, can be randomly transferred to other servers, etc. Whenever that happens, the content of your JSON file will be replaced with whatever it was when you originally uploaded it to Heroku.
What you need is something like Firebase, MongoDB, or another database. There are many that offer free versions for lightweight use cases like yours. Alternatively, you could set up port forwarding and host the server locally on your network. That way you could ensure the JSON file is always saved.
Crucially: it would be almost impossible to store data on Heroku for more than a couple of hours. Heroku just isn't designed to be able to store any data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sam Spade |
