diff --git a/pterodactyl/websocket.py b/pterodactyl/websocket.py index 9dea464..d7f874c 100644 --- a/pterodactyl/websocket.py +++ b/pterodactyl/websocket.py @@ -29,54 +29,51 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None: logger.debug("Authentication message sent") coginstance.websocket = websocket + current_status = '' - await loop(coginstance, websocket) + while True: # pylint: disable=too-many-nested-blocks + message = await websocket.recv() + if json.loads(message)['event'] in ('token expiring', 'token expired'): + logger.debug("Received token expiring/expired event. Refreshing token.") + websocket_credentials = await retrieve_websocket_credentials(coginstance) + auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]}) + await websocket.send(auth_message) + logger.debug("Authentication message sent") -async def loop(coginstance: Pterodactyl, websocket: websockets.WebSocketClientProtocol) -> None: - current_status = '' - while True: # pylint: disable=too-many-nested-blocks - message = await websocket.recv() - if json.loads(message)['event'] in ('token expiring', 'token expired'): - logger.debug("Received token expiring/expired event. Refreshing token.") - websocket_credentials = await retrieve_websocket_credentials(coginstance) - auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]}) - await websocket.send(auth_message) - logger.debug("Authentication message sent") + if json.loads(message)['event'] == 'auth success': + logger.info("WebSocket authentication successful") - if json.loads(message)['event'] == 'auth success': - logger.info("WebSocket authentication successful") + if json.loads(message)['event'] == 'console output' and await config.console_channel() is not None: + if current_status in ('running', 'offline', ''): + content = remove_ansi_escape_codes(json.loads(message)['args'][0]) - if json.loads(message)['event'] == 'console output' and await config.console_channel() is not None: - if current_status in ('running', 'offline', ''): - content = remove_ansi_escape_codes(json.loads(message)['args'][0]) - - channel = coginstance.bot.get_channel(await config.console_channel()) - if channel is not None: - if content.startswith('['): - pagified_content = pagify(content, delims=[" ", "\n"]) - for page in pagified_content: - await channel.send(content=page) - - chat_message = await check_if_chat_message(content) - if chat_message: - info = await get_info(chat_message['username']) - if info is not None: - await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], info['data']['player']['avatar']) - else: - await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png') - - server_message = await check_if_server_message(content) - if server_message: - channel = coginstance.bot.get_channel(await config.chat_channel()) + channel = coginstance.bot.get_channel(await config.console_channel()) if channel is not None: - await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...') + if content.startswith('['): + pagified_content = pagify(content, delims=[" ", "\n"]) + for page in pagified_content: + await channel.send(content=page) - if json.loads(message)['event'] == 'status': - current_status = json.loads(message)['args'][0] - if await config.console_channel() is not None: - console = coginstance.bot.get_channel(await config.console_channel()) - if console is not None: - await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`") + chat_message = await check_if_chat_message(content) + if chat_message: + info = await get_info(chat_message['username']) + if info is not None: + await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], info['data']['player']['avatar']) + else: + await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png') + + server_message = await check_if_server_message(content) + if server_message: + channel = coginstance.bot.get_channel(await config.chat_channel()) + if channel is not None: + await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...') + + if json.loads(message)['event'] == 'status': + current_status = json.loads(message)['args'][0] + if await config.console_channel() is not None: + console = coginstance.bot.get_channel(await config.console_channel()) + if console is not None: + await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`") async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[dict]: base_url = await config.base_url()