fix restarting will not backup

This commit is contained in:
2017-09-13 01:08:02 +08:00
parent cb82bb0526
commit 8e4b93f6b2

41
bot.py
View File

@ -39,6 +39,7 @@ def on_chat(msg):
if command[0] == '/restart' and str(from_id) in config.ADMIN:
bot.sendMessage(chat_id, "即將更新並重新啟動")
print("Restarting...")
backup_db()
time.sleep(1)
os._exit(0)
@ -134,12 +135,29 @@ def on_chat(msg):
else:
user.send_answer('', msg['document']['file_id'])
def backup_db():
users_backup = {}
for key in users.keys():
user = users[key]
users_backup[key] = {
'userid': user.userid,
'username': user.username,
'password': user.password,
'status': user.status,
'question': user.question
}
with open('users.json', 'w') as f:
json.dump(users_backup, f, indent=' ')
def restore_db():
with open('users.json', 'r') as f:
users_restore = json.load(f)
for key in users_restore.keys():
user = users_restore[key]
users[key] = kuser(user['userid'], user['username'], user['password'], user['status'], user['question'])
# restore
with open('users.json', 'r') as f:
users_restore = json.load(f)
for key in users_restore.keys():
user = users_restore[key]
users[key] = kuser(user['userid'], user['username'], user['password'], user['status'], user['question'])
restore_db()
# start this bot
MessageLoop(bot, on_chat).run_as_thread()
@ -152,15 +170,4 @@ while True:
bot.getMe()
# backup
users_backup = {}
for key in users.keys():
user = users[key]
users_backup[key] = {
'userid': user.userid,
'username': user.username,
'password': user.password,
'status': user.status,
'question': user.question
}
with open('users.json', 'w') as f:
json.dump(users_backup, f, indent=' ')
backup_db()