nonebot 自定义 运势 plugin
注意一个点:json文件要将 读写分开操作(两个with open操作) 合并在一起的话会出现不能覆盖原json文档的情况 (然后调半天。。。)
u_id = session.event.user_id
with open(pth, 'r+', encoding = 'utf-8') as f:
data = f.read()
if data == last_time:
with open(path, 'r+', encoding = 'utf-8') as _f:
dd = json.load(_f)
if str(u_id) in dd:
kw = dd[str(u_id)]['ys']
else:
ys = await CreateQd()
kw = ys
dd[str(u_id)]={'ys':str(ys)}
with open(path, 'r+', encoding = 'utf-8') as _f:
json.dump(dd, _f,ensure_ascii=False,indent=4)
else:
f.seek(0)
f.truncate()
f.write(last_time)
kw = await CreateQd()
with open(path, 'r', encoding = 'utf-8') as _f:
dd = json.load(_f)
if str(u_id) in dd:
dd[str(u_id)]['ys'] = kw
else:
dd[str(u_id)] ={'ys':str(kw)}
with open(path, 'r+', encoding = 'utf-8') as _f:
json.dump(dd, _f,ensure_ascii=False,indent=4)
await session.send(kw ,at_sender= True, ensure_private= True)