【问题标题】:Json data doesn't save at the end of the dayJson 数据不会在一天结束时保存
【发布时间】:2021-06-24 04:24:08
【问题描述】:

我正在使用 repl.it 来运行我的 discord 机器人,但我遇到了问题。有时,通常在一天结束时,用户所做的任何事情都不会保存在分配用于存储数据的 json 文件中。有谁知道发生了什么?

【问题讨论】:

    标签: python json discord.py repl.it


    【解决方案1】:

    嘿,我也遇到了同样的问题,这是因为如果您不在编辑器中,文件更改不会持续存在。

    您能做的最好的事情就是使用数据库来存储您的数据。

    看看一些数据库,比如 mongo db 和 PostgreSQL。

    【讨论】:

      【解决方案2】:

      我已找到解决此问题的方法。显然,repl.it 由于发生了 DDos 攻击而强制执行此功能,我不太确定发生了什么,但它是一项安全功能。要保存文件,即使我不在编辑器上(导致问题),我也必须让我的脚本打开文件然后关闭它,保存它。我是这样做的:

      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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多