简体   繁体   中英

Json data doesn't save at the end of the day

I 'm using repl.it to run my discord bot but I have encountered a problem. Sometimes, at the end of the day usually, whatever the users did does not save in the json file assigned to store the data. Does anyone know what is happening?

Hey i have had the same problem and it is because file changes do not persist if you are not in the editor.

The best thing you could do is use a database to store your data.

Take a look at some databases like mongo db and PostgreSQL.

I have found a fix to this problem. Apparently, repl.it enforced this feature due to DDos attack occurring, I'm not really sure what happened but it was a security feature. To save the file, even when I'm not on the editor (which was causing the problem) I have to make my script open the file and then close it, saving it. Here's how I did that:

def save(filePath="filename.json", **kwargs):
  while True:
    with open(filePath, "w") as f:
      f.write(json.dumps(kwargs))

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Streaming(
        name=f"+help - {len(bot.guilds)} servers - dsc.gg/wumpusbot",
        url="https://www.twitch.tv/defaultmodels"))
    print('Bot is online')
    await save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM