一段时间后,Json 丢失数据?还是我的代码中的错误?

问题描述

下面是我的代码,这个系统是一个积分系统,当用户说话时,他们会得到积分!这个系统可以工作,但是过了一会儿,当我使用 ;points 命令时,它说用户是新用户,因为 try except 函数正在被触发,这是由于 try except 有意屏蔽了一个键错误。如果早点工作,为什么会突然拉出一个关键错误?我还包含了我的 JSON 以提供帮助!

Discord.py 主脚本 - 包括在消息上调用的点数提供者,以及在 ;points 上调用的点数审查者

  @Cog.listener()
  async def on_message(self,message):
    servername = message.guild.name
    guildid = message.guild.id
    serverid = guildid
    name = message.author
    userid = message.author.id

    points = {}
    with open("./points.json","r") as f:
      points = json.load(f)
    try:
      existingpoints = points[str(userid)][str(guildid)]
      newpoints = existingpoints + 1
      points[str(userid)][str(guildid)] = newpoints
    except:
      #points[str(userid)] = {}
      points[str(userid)][str(guildid)] = 1


    try:
      existingpoints = points[str(userid)][str(guildid)]
      if existingpoints % 50 == 0:
        await message.channel.send(f"{message.author.mention} has levelled up with {existingpoints} points!")
    except:
      pass

    with open("./points.json","w") as f:
      json.dump(points,f)

    
   



  @command(name="points",aliases=["earnings"])
  async def points(self,ctx,leaderboard=None):
    servername = ctx.guild.name
    guildid = ctx.guild.id
    serverid = guildid
    name = ctx.author
    userid = ctx.author.id
    
    points = {}
    with open("./points.json","r") as f:
      points = json.load(f)
    if leaderboard == None:
      leaderboard = ("none")
      try:
        existingpoints = points[str(userid)][str(guildid)]
        await ctx.send(f"{ctx.author.display_name} has {existingpoints} points.")
      except:
        await ctx.send(f"You are new! Never spoken a word here before! Wow :slight_smile: Begin talking to earn points,then try again!")
    else:
      embed = discord.Embed(title=f"Points Leaderboard",description= "",color=0x00ff00)
      for i in points.keys():
        points = points[i][str(guildid)]
        user = await self.get_user_info(i)
        name = user.name
        embed.add_field(name=f"{name}",value=f"{points}")
      await ctx.send(embed=embed)

JSON:

{"719588575733350551": {"777618366894571530": 12,"727236037742690354": 3,"762046745248268298": 8,"799671710965694475": 7},"738357845543616614": {"777618366894571530": 8,"727236037742690354": 2,"762046745248268298": 3,"799671710965694475": 1,"715310006710435900": 2},"695439575329275945": {"762046745248268298": 8},"758272354173845536": {"762046745248268298": 5},"438762249809821699": {"715310006710435900": 81},"155149108183695360": {"715310006710435900": 1},"757625487353839749": {"715310006710435900": 3},"719770082728738867": {"715310006710435900": 3},"789522260205240350": {"762046745248268298": 4},"729667553491812403": {"762046745248268298": 50,"398601531525562369": {"793109512638824458": 2},"508968886998269962": {"715310006710435900": 2},"318567688655863810": {"394711355543781378": 1},"408754269127180288": {"715310006710435900": 2},"760720870934708244": {"762046745248268298": 3},"690965766853361727": {"715310006710435900": 2},"437808476106784770": {"799671710965694475": 1},"648567468549472257": {"762046745248268298": 1,"799671710965694475": 2},"705016654341472327": {"394711355543781378": 1}}

解决方法

您正在丢失里面的数据,因为您对文件的处理不当。打开 JSON 文件后尝试关闭它。不关闭文件是不好的做法,它会减慢您的程序,并且数据通常缓存在内存中并且在文件关闭之前不会击中硬盘驱动器。文件打开的时间越长,丢失数据的可能性就越大。您还会在 on_message 中打开 JSON 文件,这是难以置信的危险。如果这样做,您可能会达到一次可以打开的文件数量的限制。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...