fix restarting will not backup
This commit is contained in:
41
bot.py
41
bot.py
@ -39,6 +39,7 @@ def on_chat(msg):
|
|||||||
if command[0] == '/restart' and str(from_id) in config.ADMIN:
|
if command[0] == '/restart' and str(from_id) in config.ADMIN:
|
||||||
bot.sendMessage(chat_id, "即將更新並重新啟動")
|
bot.sendMessage(chat_id, "即將更新並重新啟動")
|
||||||
print("Restarting...")
|
print("Restarting...")
|
||||||
|
backup_db()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
||||||
@ -134,12 +135,29 @@ def on_chat(msg):
|
|||||||
else:
|
else:
|
||||||
user.send_answer('', msg['document']['file_id'])
|
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
|
# restore
|
||||||
with open('users.json', 'r') as f:
|
restore_db()
|
||||||
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'])
|
|
||||||
|
|
||||||
# start this bot
|
# start this bot
|
||||||
MessageLoop(bot, on_chat).run_as_thread()
|
MessageLoop(bot, on_chat).run_as_thread()
|
||||||
@ -152,15 +170,4 @@ while True:
|
|||||||
bot.getMe()
|
bot.getMe()
|
||||||
|
|
||||||
# backup
|
# backup
|
||||||
users_backup = {}
|
backup_db()
|
||||||
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=' ')
|
|
Reference in New Issue
Block a user