fix(pterodactyl): removed ansi escape codes
Some checks failed
Actions / Lint Code (Ruff & Pylint) (pull_request) Failing after 19s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 20s

This commit is contained in:
Seaswimmer 2024-02-28 13:21:39 -05:00
parent c740cdc6dc
commit ec2cd09f8e
Signed by: cswimr
GPG key ID: B8953EC01E5C4063

View file

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