From 0d21a52ce3039a45a90ba68ddc9dd3af480f9ea1 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Thu, 29 Feb 2024 23:50:54 -0500 Subject: [PATCH] feat(pterodactyl): added support for bridging messages sent by the server --- pterodactyl/config.py | 1 + pterodactyl/pterodactyl.py | 12 ++++++++++++ pterodactyl/websocket.py | 16 ++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/pterodactyl/config.py b/pterodactyl/config.py index 76a0e51..1fdc66b 100644 --- a/pterodactyl/config.py +++ b/pterodactyl/config.py @@ -12,6 +12,7 @@ def register_config(config_obj: Config) -> None: startup_arguments=None, power_action_in_progress=False, 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"}]', api_endpoint="minecraft", chat_channel=None diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index 0e05ff1..f67a756 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -153,3 +153,15 @@ class Pterodactyl(commands.Cog): return await ctx.send(f"Chat command is currently set to:\n{box(command, 'json')}") await config.chat_command.set(command) await ctx.send(f"Chat command set to:\n{box(command, 'json')}") + + @pterodactyl_config_chat.command(name = "server") + async def pterodactyl_config_chat_server(self, ctx: commands.Context, *, regex: str = None) -> None: + """Set the regex pattern to match server messages on the server. + + See [documentation]() for more information.""" + #TODO - fix this link + if regex is None: + regex = await config.server_regex() + return await ctx.send(f"Server regex is currently set to:\n{box(regex, 'regex')}") + await config.server_regex.set(regex) + await ctx.send(f"Server regex set to:\n{box(regex, 'regex')}") diff --git a/pterodactyl/websocket.py b/pterodactyl/websocket.py index e4be910..9dea464 100644 --- a/pterodactyl/websocket.py +++ b/pterodactyl/websocket.py @@ -65,6 +65,12 @@ async def loop(coginstance: Pterodactyl, websocket: websockets.WebSocketClientPr 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: @@ -99,6 +105,16 @@ def remove_ansi_escape_codes(text: str) -> str: #NOTE - https://chat.openai.com/share/d92f9acf-d776-4fd6-a53f-b14ac15dd540 return ansi_escape.sub('', text) +async def check_if_server_message(text: str) -> Union[bool, str]: + logger.debug("Checking if message is a server message") + regex = await config.server_regex() + match: Optional[re.Match[str]] = re.match(regex, text) + if match: + logger.debug("Message is a server message") + return match.group(1) + logger.debug("Message is not a server message") + return False + async def check_if_chat_message(text: str) -> Union[bool, dict]: logger.debug("Checking if message is a chat message") regex = await config.chat_regex()