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")