json - 一段时间后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, "715310006710435900": 2}, "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文件。如果这样做,您可能会达到一次可以打开多少个文件的限制。
推荐阅读
- visual-studio-2017 - 在 EF Core、ASP.NET Core MVC 中实现 Table Per Hierarchy Inheritance 的最佳方法
- html - 孩子的保证金问题
- php - WordPress 自定义帖子类型数组未按预期工作
- python - 时间读取为时间增量,前面有大量天数
- java - Java ResultSet 添加零
- java - Java / Android 中的嵌套 setOnClickListener
- java - java - 如何在一行中为Java数组中的范围设置相同的值?
- python - 烧瓶迁移未检测到 models.py
- pymc - pymc3 多项式混合卡住了
- ruby-on-rails - 设置后仍然欢迎使用 ngnix