11import threading
22import shlex
33
4- from . import cloudlink
4+ from .Cloudlink import CloudLink
55import sys
66
77import json
1818import time
1919import logging
2020
21+ from websocket ._exceptions import WebSocketConnectionClosedException
22+
2123
2224class Bot :
2325 """
@@ -38,8 +40,8 @@ def _t_ping(self):
3840
3941 self .wss .sendPacket ({"cmd" : "ping" , "val" : "" })
4042
41- def __init__ (self , prefix = None ):
42- self .wss = cloudlink . CloudLink ()
43+ def __init__ (self , prefix = None , autoreload : int or None = None ):
44+ self .wss = CloudLink ()
4345 self .callbacks = {}
4446
4547 self .wss .callback (
@@ -56,11 +58,21 @@ def __init__(self, prefix=None):
5658 self .password = None
5759 self .logger_in = False
5860
61+ if autoreload :
62+ self .autoreload = True
63+ self .autoreload_time = autoreload + 1
64+ self .autoreload_original = autoreload + 1
65+ else :
66+ self .autoreload = False
67+ self .autoreload_time = 0
68+ self .autoreload_original = 0
69+
5970 self .commands = {}
6071 self .prefix = prefix
6172 self ._t_ping_thread = threading .Thread (target = self ._t_ping , daemon = True ) # (:
6273 self .logger = logging .getLogger ("MeowerBot" )
6374 self .bad_exit = False
75+ self .server = None
6476
6577 self .cogs = {}
6678
@@ -84,6 +96,13 @@ def run_cb(self, cbid, args=(), kwargs=None): # cq: ignore
8496
8597 def __handle_error__ (self , e ):
8698 self .run_cb ("error" , args = (e ))
99+ if type (e ) == WebSocketConnectionClosedException and self .autoreload :
100+ self .__handle_close__ ()
101+ return
102+
103+
104+
105+
87106
88107 def _debug_fix (self , packet ):
89108 packet = json .loads (packet ) # Server bug workaround
@@ -197,7 +216,8 @@ def _handle_status(self, status, listener):
197216 elif listener == "__meowerbot__send_message" :
198217 if status == "I:100 | OK" :
199218 return # This is just checking if a post went OK
200-
219+
220+ self .autoreload_time = self .autoreload_original #autoreload time should reset if
201221 raise RuntimeError ("Post Failed to send" )
202222
203223 def callback (self , callback , cbid = None ):
@@ -210,6 +230,22 @@ def callback(self, callback, cbid=None):
210230 self .callbacks [cbid ].append (callback )
211231
212232 def __handle_close__ (self , * args , ** kwargs ):
233+ if self .autoreload :
234+ self .autoreload = False #to stop race condisons
235+ self .logger_in = True
236+ if not self .autoreload_time >= 100 : self .autoreload_time *= 1.2
237+
238+ else :
239+ self .autoreload_time = 100
240+
241+
242+ time .sleep (self .autoreload_time )
243+ self .autoreload = True #reset this, as i set it to false above.
244+
245+ self .wss .state = 0
246+ self .wss .client (self .server )
247+ return #dont want the close callback to be called here
248+
213249 self .run_cb ("close" , args = args , kwargs = kwargs )
214250
215251 def __handle_packet__ (self , packet ):
@@ -335,6 +371,7 @@ def run(self, username, password, server="wss://server.meower.org"):
335371 if self .prefix is None :
336372 self .prefix = "@" + self .username
337373 self .logger = logging .getLogger (f"MeowerBot { self .username } " )
374+ self .server = server
338375 self .wss .client (server )
339376
340377 if self .bad_exit :
0 commit comments