0

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.

  • The only solution is to stop using replit. Making the extra thread might not fully allow the async things to run. – Eric Jin May 24 '22 at 18:04
  • This is exactly something I didn´t want to hear, but thank you anyway. –  May 24 '22 at 19:41

1 Answers1

0

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

AngleBoost
  • 47
  • 1
  • 6