even better
This commit is contained in:
parent
1d958a5b09
commit
f57b2cbd0f
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
|||||||
|
# Persistence
|
||||||
|
persistent_vars.json
|
||||||
|
|
||||||
# API-Key (keep secret at all times)
|
# API-Key (keep secret at all times)
|
||||||
keys.py
|
keys.py
|
||||||
|
|
||||||
|
@ -40,8 +40,7 @@ class TelegramIO():
|
|||||||
def handle_result(self, result):
|
def handle_result(self, result):
|
||||||
"""Inspects the message and reacts accordingly. Can easily be extended"""
|
"""Inspects the message and reacts accordingly. Can easily be extended"""
|
||||||
for message_data in result:
|
for message_data in result:
|
||||||
message_read = self.persistence.read("messages_read")
|
self.persistence.increment("messages_read")
|
||||||
self.persistence.write("messages_read", message_read + 1)
|
|
||||||
self.offset = message_data["update_id"] + 1
|
self.offset = message_data["update_id"] + 1
|
||||||
message = message_data["message"]
|
message = message_data["message"]
|
||||||
self.message_id = message["message_id"]
|
self.message_id = message["message_id"]
|
||||||
|
2
main.py
2
main.py
@ -22,7 +22,7 @@ class ChatBot():
|
|||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
# Persistent variable
|
# Persistent variable
|
||||||
self.persistence = pvars.Variables("persistence/permanent_vars.json")
|
self.persistence = pvars.Variables("persistence/persistent_vars.json", "persistence/persistent_init.json")
|
||||||
# Uptime counter
|
# Uptime counter
|
||||||
self.start_time = datetime.datetime.now()
|
self.start_time = datetime.datetime.now()
|
||||||
self.persistence.increment("reboots")
|
self.persistence.increment("reboots")
|
||||||
|
@ -1 +0,0 @@
|
|||||||
{"messages_read": 66, "messages_sent": 41, "commands_executed": 31, "photos_sent": 6, "log": [], "chat_members": {"364520272": "Remy Moll"}, "reboots": 57}
|
|
1
persistence/persistent_init.json
Normal file
1
persistence/persistent_init.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"messages_read": 0, "messages_sent": 0, "commands_executed": 0, "photos_sent": 0, "log": [], "chat_members": {}, "reboots": 0}
|
@ -1,20 +1,18 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
class Variables():
|
class Variables():
|
||||||
""""""
|
""""""
|
||||||
|
|
||||||
def __init__(self,savefile_path):
|
def __init__(self,savefile_path, init_path):
|
||||||
self.path = savefile_path
|
self.path = savefile_path
|
||||||
|
self.init_path = init_path
|
||||||
self.last_action = ""
|
self.last_action = ""
|
||||||
# last performed action, if only reads are made, then the underlying var has not been changed
|
# last performed action, if only reads are made, then the underlying var has not been changed
|
||||||
# and doesn't need to be read again
|
# and doesn't need to be read again
|
||||||
self.savefile = {}
|
self.savefile = {}
|
||||||
|
|
||||||
def write(self, name, value):
|
def write(self, name, value):
|
||||||
if self.last_action == "read":
|
|
||||||
pre = self.savefile
|
|
||||||
else:
|
|
||||||
pre = self.read()
|
pre = self.read()
|
||||||
pre[name] = value
|
pre[name] = value
|
||||||
try:
|
try:
|
||||||
@ -25,12 +23,20 @@ class Variables():
|
|||||||
except:
|
except:
|
||||||
print("Config not written - critical")
|
print("Config not written - critical")
|
||||||
|
|
||||||
|
|
||||||
def read(self, name=""):
|
def read(self, name=""):
|
||||||
if self.last_action == "read":
|
if self.last_action == "read":
|
||||||
vars = self.savefile
|
if name == "":
|
||||||
|
return self.savefile
|
||||||
else:
|
else:
|
||||||
|
return self.savefile[name]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
file = open(self.path,"r")
|
if os.path.exists(self.path):
|
||||||
|
file_path = self.path
|
||||||
|
else:
|
||||||
|
file_path = self.init_path
|
||||||
|
file = open(file_path,"r")
|
||||||
vars = json.load(file)
|
vars = json.load(file)
|
||||||
file.close()
|
file.close()
|
||||||
self.savefile = vars
|
self.savefile = vars
|
||||||
@ -38,22 +44,12 @@ class Variables():
|
|||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if len(name) != 0:
|
if name != "":
|
||||||
vars = vars[name]
|
vars = vars[name]
|
||||||
|
|
||||||
return vars
|
return vars
|
||||||
|
|
||||||
def increment(self, name=""):
|
|
||||||
if self.last_action == "read":
|
|
||||||
pre = self.savefile
|
|
||||||
else:
|
|
||||||
pre = self.read()
|
|
||||||
|
|
||||||
try:
|
def increment(self, name=""):
|
||||||
pre[name] += 1
|
pre = self.read(name)
|
||||||
file = open(self.path,"w")
|
plus1 = pre + 1
|
||||||
json.dump(pre, file)
|
self.write(name, plus1)
|
||||||
file.close()
|
|
||||||
self.last_action = "write"
|
|
||||||
except:
|
|
||||||
print("Config not written - critical")
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user