Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit e70aa26

Browse files
autoreconnect cuz i finally desided its worth it
1 parent c30466f commit e70aa26

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

MeowerBot/Bot.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import threading
22
import shlex
33

4-
from . import cloudlink
4+
from .Cloudlink import CloudLink
55
import sys
66

77
import json
@@ -18,6 +18,8 @@
1818
import time
1919
import logging
2020

21+
from websocket._exceptions import WebSocketConnectionClosedException
22+
2123

2224
class 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:

tests/command.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
basicConfig(level=DEBUG)
77

8-
bot: Bot = Bot(prefix="/")
8+
bot: Bot = Bot(prefix="/", autoreload=1)
99

1010
@bot.command()
1111
def test(ctx, *args):
1212
ctx.send_msg(" ".join(args) + "\n mb.py " + __version__)
1313

14+
@bot.command(args=0)
15+
def reloadtime(ctx):
16+
ctx.send_msg(f"My reload time is {round(bot.autoreload_time)}" )
17+
1418
bot.run(env['username'], env['password'])
1519

0 commit comments

Comments
 (0)