'Discord.py 2.0 on Repl.it

I want to make discord bot that uses buttons I downloaded library and ewerything. It works but because I am using Repl.it i have to use web + uptimerobot so it will never turn off. Even after that it seems every few hours to quickly turn off and back on, so it will lose all data except databases. I was thinking i can solve this by saving message to database and inside event on_ready delete message and create new one but bot after going off and on cant delete message from varriable.

Code for web:

from flask import Flask
from threading import Thread

app = Flask('')

@app.route('/')
def home():
    return "I am working!"

def run():
  app.run(host='0.0.0.0',port=8080)

def keep_alive():
    t = Thread(target=run)
    t.start()

I would like to solve turning off and on problem + it will be helpfull to fix deleting message from database after reseting.

Side question: Do you guys know any easy way to assign instead of reference value to varriable because of circular reference/dependency.



Solution 1:[1]

Make a JSON file to save the data and just have it not delete the data on restart. have it autosave when a user uses it.

kinda like this

@client.command()
async def add(ctx, meme_):
    def add_memes(meme, file="memes.json"):
        with open(file, "r+") as fw:
            j = json.load(fw)
            j["memes"].append(meme)
            with open(file, "w+") as wp:
                wp.write(json.dumps(j))
    try:
        with open("memes.json", "r"):
            pass
    except:
        with open("memes.json", "w+") as wp:
            wp.write('{"memes" : []}')
    finally:
        add_memes(meme_)
        await ctx.send("Done!")

but this is a command for saving memes

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