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