From ec2cd09f8ebaa4ca051fd98240972dee3fa5c686 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Wed, 28 Feb 2024 13:21:39 -0500 Subject: [PATCH] fix(pterodactyl): removed ansi escape codes --- pterodactyl/pterodactyl.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index 2a2330b..5ef74da 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -1,5 +1,6 @@ import json import logging +import re import discord import websockets @@ -77,13 +78,19 @@ class Pterodactyl(commands.Cog): if json.loads(message)['event'] == 'console output' and await self.config.console_channel() is not None: channel = self.bot.get_channel(await self.config.console_channel()) if channel is not None: - await channel.send(json.loads(message)['args'][0][:1900]) + content = self.remove_ansi_escape_codes(json.loads(message)['args'][0][:1900]) + await channel.send(content=content) #TODO - Add pagification for long messages to prevent Discord API errors except websockets.exceptions.ConnectionClosed as e: self.logger.debug("WebSocket connection closed: %s", e) websocket_credentials = client.servers.get_websocket(server_id) continue + def remove_ansi_escape_codes(self, text: str): + ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') + #NOTE - https://chat.openai.com/share/d92f9acf-d776-4fd6-a53f-b14ac15dd540 + return ansi_escape.sub('', text) + def get_task(self): return self.bot.loop.create_task(self.establish_websocket_connection(), name="Pterodactyl Websocket Connection")