From d39f0ba10476a80d7457666838fd6a85dc6ba09c Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 1 Mar 2024 00:02:42 -0500 Subject: [PATCH] feat(pterodactyl): server status will persist through reloads --- pterodactyl/config.py | 2 +- pterodactyl/websocket.py | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pterodactyl/config.py b/pterodactyl/config.py index bcf6e77..7af450b 100644 --- a/pterodactyl/config.py +++ b/pterodactyl/config.py @@ -10,7 +10,7 @@ def register_config(config_obj: Config) -> None: console_channel=None, startup_jar=None, startup_arguments=None, - power_action_in_progress=False, + current_status='', chat_regex=r"\[(\d{2}:\d{2}:\d{2})\sINFO\]:\s<(\w+)>\s(.*)", server_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: \[Not Secure\] \[.*\] (.*)", chat_command='tellraw @a ["",{"text":".$U ","color":".$C"},{"text":" (DISCORD): ","color":"blue"},{"text":".$M","color":"white"}]', diff --git a/pterodactyl/websocket.py b/pterodactyl/websocket.py index d7f874c..3e4daae 100644 --- a/pterodactyl/websocket.py +++ b/pterodactyl/websocket.py @@ -29,7 +29,6 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None: logger.debug("Authentication message sent") coginstance.websocket = websocket - current_status = '' while True: # pylint: disable=too-many-nested-blocks message = await websocket.recv() @@ -44,7 +43,7 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None: 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', ''): + if await config.current_status() in ('running', 'offline', ''): content = remove_ansi_escape_codes(json.loads(message)['args'][0]) channel = coginstance.bot.get_channel(await config.console_channel()) @@ -69,11 +68,23 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None: await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...') if json.loads(message)['event'] == 'status': + old_status = await config.current_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]}`") + if old_status != current_status: + await config.current_status.set(current_status) + 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! `{current_status}`") + if await config.chat_channel() is not None: + if current_status == 'running' and await config.startup_msg() is not None: + chat = coginstance.bot.get_channel(await config.chat_channel()) + if chat is not None: + await chat.send(await config.startup_msg()) + if current_status == 'stopping' and await config.shutdown_msg() is not None: + chat = coginstance.bot.get_channel(await config.chat_channel()) + if chat is not None: + await chat.send(await config.shutdown_msg()) async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[dict]: base_url = await config.base_url()