From 958b660adce4e1e136593d9d103514fe42d04715 Mon Sep 17 00:00:00 2001 From: zRitsu <74823568+zRitsu@users.noreply.github.com> Date: Sat, 18 Dec 2021 08:38:21 -0300 Subject: [PATCH] =?UTF-8?q?corre=C3=A7=C3=B5es=20e=20pequenas=20melhorias.?= =?UTF-8?q?..?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpc_client.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/rpc_client.py b/rpc_client.py index 9e1113a..3a7d55f 100644 --- a/rpc_client.py +++ b/rpc_client.py @@ -122,6 +122,10 @@ def update(self, user_id: int, bot_id: int, data: dict): self.check_presence(user_id, bot_id) + if not data: + self.users_rpc[user_id][bot_id].clear() + return + payload = { "assets": { "large_image": "app" @@ -133,6 +137,8 @@ def update(self, user_id: int, bot_id: int, data: dict): info = data.pop("info", None) + payload.update(data) + if info and track: m = info["members"] @@ -252,11 +258,7 @@ def update(self, user_id: int, bot_id: int, data: dict): if buttons: payload["buttons"] = buttons - self.users_rpc[user_id][bot_id].update_activity(payload) - - else: - self.users_rpc[user_id][bot_id].clear() - return + self.users_rpc[user_id][bot_id].update_activity(payload) def get_lang(self, key: str) -> str: @@ -278,6 +280,8 @@ async def handle_socket(self, uri): try: async with websockets.connect(uri) as ws: + print(f"Websocket conectado: {uri}") + for i in user_clients: await ws.send(json.dumps({"user_id": i})) @@ -295,7 +299,7 @@ async def handle_socket(self, uri): op = data.pop("op") public = data.pop("public", True) - bot_id = data.get("bot_id") + bot_id = data.pop("bot_id", None) user = user_clients[user_id]["user"] print(f"op: {op} | {user} [{user_id}] | bot: {bot_id}") @@ -341,6 +345,7 @@ async def handle_socket(self, uri): self.get_lang("listeners") + f': {m}' except KeyError: pass + self.update(user_id, bot_id, data) case "close": @@ -350,15 +355,16 @@ async def handle_socket(self, uri): print(f"unknow op: {msg.data}") - except (websockets.ConnectionClosedError, ConnectionRefusedError, ConnectionResetError, OSError): - print(f"Conexão perdida com o servidor: {uri} | Reconectando em 60seg.") + except (websockets.ConnectionClosedError, ConnectionResetError) as e: + print(f"Conexão perdida com o servidor: {uri} | Reconectando em 60seg. {repr(e)}") await asyncio.sleep(60) + except ConnectionRefusedError: + await asyncio.sleep(500) except Exception as e: - traceback.print_exc() print(f"Erro na conexão: {uri} | {repr(e)}") await asyncio.sleep(60) async def handler(self): await asyncio.wait([asyncio.create_task(self.handle_socket(uri)) for uri in config["urls"]]) -asyncio.get_event_loop().run_until_complete(RpcClient().handler()) \ No newline at end of file +asyncio.run(RpcClient().handler()) \ No newline at end of file