Json 一段时间后丢失数据?还是我的代码中有错误?
Json loosing data after a period of time? or is it an error in my code?
下面是我的代码,这个系统是一个积分系统,当用户说话时他们得到积分!这个系统可以工作,但是过了一会儿,当我使用 ;points
命令时,它说用户是新用户,因为正在触发 try except 函数,这是 try except 有意掩盖的键错误的结果。如果它更早工作,为什么会突然出现关键错误?我还附上了我的 JSON 以提供帮助!
Discord.py 主脚本 - 包括消息中调用的点数提供者,以及点数评论者中调用的;点数
@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 文件,这 非常危险 。如果这样做,您可能会达到一次可以打开的文件数量的限制。
下面是我的代码,这个系统是一个积分系统,当用户说话时他们得到积分!这个系统可以工作,但是过了一会儿,当我使用 ;points
命令时,它说用户是新用户,因为正在触发 try except 函数,这是 try except 有意掩盖的键错误的结果。如果它更早工作,为什么会突然出现关键错误?我还附上了我的 JSON 以提供帮助!
Discord.py 主脚本 - 包括消息中调用的点数提供者,以及点数评论者中调用的;点数
@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 文件,这 非常危险 。如果这样做,您可能会达到一次可以打开的文件数量的限制。