Compare commits

..

No commits in common. "37cdca09ecb7521591ff0fc3116927f1afdeb814" and "de5d0194c8ae099d927ecf93237f79960dd741a8" have entirely different histories.

3 changed files with 51 additions and 64 deletions

View file

@ -1,16 +0,0 @@
# Pterodactyl
/// admonition | This project is in active development
type: warning
These docs are not complete yet, and there is a lot still to do.
///
Pterodactyl allows for connecting to a Pterodactyl server through websockets. It is intended primarily for use with Minecraft servers, as it allows for version & server platform-agnostic Discord integration, including console logging and two-way chat bridging.
## Installation
```bash
[p]repo add seacogs https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs
[p]cog install seacogs pterodactyl
[p]cog load aurora
```

View file

@ -19,8 +19,6 @@ nav:
- Bible: bible.md - Bible: bible.md
- Backup: backup.md - Backup: backup.md
- Nerdify: nerdify.md - Nerdify: nerdify.md
- Pterodactyl:
- pterodactyl/index.md
plugins: plugins:
- git-authors - git-authors

View file

@ -59,7 +59,8 @@ class Pterodactyl(commands.Cog):
except exceptions.PterodactylApiError as e: except exceptions.PterodactylApiError as e:
return self.logger.error('Failed to retrieve Pterodactyl websocket: %s', e) return self.logger.error('Failed to retrieve Pterodactyl websocket: %s', e)
async with websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60) as websocket: async for websocket in websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60):
try:
self.logger.info("WebSocket connection established") self.logger.info("WebSocket connection established")
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]}) auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
@ -71,7 +72,7 @@ class Pterodactyl(commands.Cog):
while True: while True:
message = await websocket.recv() message = await websocket.recv()
if json.loads(message)['event'] in ('token expiring', 'token expired'): if json.loads(message)['event'] in ['token expiring', 'token expired']:
self.logger.debug("Received token expiring/expired event. Refreshing token.") self.logger.debug("Received token expiring/expired event. Refreshing token.")
websocket_credentials = client.servers.get_websocket(server_id) websocket_credentials = client.servers.get_websocket(server_id)
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]}) auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
@ -82,7 +83,7 @@ class Pterodactyl(commands.Cog):
self.logger.info("WebSocket authentication successful") self.logger.info("WebSocket authentication successful")
if json.loads(message)['event'] == 'console output' and await self.config.console_channel() is not None: if json.loads(message)['event'] == 'console output' and await self.config.console_channel() is not None:
if current_status in ('running', 'offline', ''): if current_status == 'running' or current_status == 'offline' or current_status == '':
content = self.remove_ansi_escape_codes(json.loads(message)['args'][0]) content = self.remove_ansi_escape_codes(json.loads(message)['args'][0])
channel = self.bot.get_channel(await self.config.console_channel()) channel = self.bot.get_channel(await self.config.console_channel())
@ -106,6 +107,10 @@ class Pterodactyl(commands.Cog):
console = self.bot.get_channel(await self.config.console_channel()) console = self.bot.get_channel(await self.config.console_channel())
if console is not None: if console is not None:
await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`") await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`")
except (websockets.exceptions.ConnectionClosed) as e:
self.logger.info("WebSocket connection closed: %s", e)
websocket_credentials = client.servers.get_websocket(server_id)
continue
def remove_ansi_escape_codes(self, text: str) -> str: def remove_ansi_escape_codes(self, text: str) -> str:
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
@ -117,9 +122,9 @@ class Pterodactyl(commands.Cog):
regex = await self.config.chat_regex() regex = await self.config.chat_regex()
match: Optional[re.Match[str]] = re.match(regex, text) match: Optional[re.Match[str]] = re.match(regex, text)
if match: if match:
groups = {"time": match.group(1), "username": match.group(2), "message": match.group(3)} dict = {"time": match.group(1), "username": match.group(2), "message": match.group(3)}
self.logger.debug("Message is a chat message\n%s", json.dumps(groups)) self.logger.debug("Message is a chat message\n%s", json.dumps(dict))
return groups return dict
self.logger.debug("Message is not a chat message") self.logger.debug("Message is not a chat message")
return False return False
@ -131,6 +136,7 @@ class Pterodactyl(commands.Cog):
if response.status == 200: if response.status == 200:
self.logger.debug("Player info retrieved for %s\n%s", username, json.dumps(await response.json())) self.logger.debug("Player info retrieved for %s\n%s", username, json.dumps(await response.json()))
return await response.json() return await response.json()
else:
self.logger.error("Failed to retrieve player info for %s: %s", username, response.status) self.logger.error("Failed to retrieve player info for %s: %s", username, response.status)
return None return None
@ -164,9 +170,8 @@ class Pterodactyl(commands.Cog):
fut.result() fut.result()
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
except Exception as e: # pylint: disable=broad-exception-caught except Exception as e:
self.logger.error("WebSocket task has failed: %s", e, exc_info=e) self.logger.error("WebSocket task has failed: %s", e, exc_info=e)
self.task.cancel()
self.task = self.get_task() self.task = self.get_task()
async def cog_load(self): async def cog_load(self):