Compare commits
4 commits
de5d0194c8
...
37cdca09ec
Author | SHA1 | Date | |
---|---|---|---|
37cdca09ec | |||
84b0201662 | |||
839ead56eb | |||
9e15730af9 |
3 changed files with 64 additions and 51 deletions
16
.docs/pterodactyl/index.md
Normal file
16
.docs/pterodactyl/index.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# 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
|
||||
```
|
|
@ -19,6 +19,8 @@ nav:
|
|||
- Bible: bible.md
|
||||
- Backup: backup.md
|
||||
- Nerdify: nerdify.md
|
||||
- Pterodactyl:
|
||||
- pterodactyl/index.md
|
||||
|
||||
plugins:
|
||||
- git-authors
|
||||
|
|
|
@ -59,58 +59,53 @@ class Pterodactyl(commands.Cog):
|
|||
except exceptions.PterodactylApiError as e:
|
||||
return self.logger.error('Failed to retrieve Pterodactyl websocket: %s', e)
|
||||
|
||||
async for websocket in websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60):
|
||||
try:
|
||||
self.logger.info("WebSocket connection established")
|
||||
async with websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60) as websocket:
|
||||
self.logger.info("WebSocket connection established")
|
||||
|
||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
||||
await websocket.send(auth_message)
|
||||
self.logger.debug("Authentication message sent")
|
||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
||||
await websocket.send(auth_message)
|
||||
self.logger.debug("Authentication message sent")
|
||||
|
||||
self.websocket = websocket
|
||||
current_status = ''
|
||||
self.websocket = websocket
|
||||
current_status = ''
|
||||
|
||||
while True:
|
||||
message = await websocket.recv()
|
||||
if json.loads(message)['event'] in ['token expiring', 'token expired']:
|
||||
self.logger.debug("Received token expiring/expired event. Refreshing token.")
|
||||
websocket_credentials = client.servers.get_websocket(server_id)
|
||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
||||
await websocket.send(auth_message)
|
||||
self.logger.debug("Authentication message sent")
|
||||
while True:
|
||||
message = await websocket.recv()
|
||||
if json.loads(message)['event'] in ('token expiring', 'token expired'):
|
||||
self.logger.debug("Received token expiring/expired event. Refreshing token.")
|
||||
websocket_credentials = client.servers.get_websocket(server_id)
|
||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
||||
await websocket.send(auth_message)
|
||||
self.logger.debug("Authentication message sent")
|
||||
|
||||
if json.loads(message)['event'] == 'auth success':
|
||||
self.logger.info("WebSocket authentication successful")
|
||||
if json.loads(message)['event'] == 'auth success':
|
||||
self.logger.info("WebSocket authentication successful")
|
||||
|
||||
if json.loads(message)['event'] == 'console output' and await self.config.console_channel() is not None:
|
||||
if current_status == 'running' or current_status == 'offline' or current_status == '':
|
||||
content = self.remove_ansi_escape_codes(json.loads(message)['args'][0])
|
||||
if json.loads(message)['event'] == 'console output' and await self.config.console_channel() is not None:
|
||||
if current_status in ('running', 'offline', ''):
|
||||
content = self.remove_ansi_escape_codes(json.loads(message)['args'][0])
|
||||
|
||||
channel = self.bot.get_channel(await self.config.console_channel())
|
||||
if channel is not None:
|
||||
if content.startswith('['):
|
||||
pagified_content = pagify(content, delims=[" ", "\n"])
|
||||
for page in pagified_content:
|
||||
await channel.send(content=page)
|
||||
channel = self.bot.get_channel(await self.config.console_channel())
|
||||
if channel is not None:
|
||||
if content.startswith('['):
|
||||
pagified_content = pagify(content, delims=[" ", "\n"])
|
||||
for page in pagified_content:
|
||||
await channel.send(content=page)
|
||||
|
||||
chat_message = await self.check_if_chat_message(content)
|
||||
if chat_message:
|
||||
info = await self.get_info(chat_message['username'])
|
||||
if info is not None:
|
||||
await self.send_chat_discord(chat_message['username'], chat_message['message'], info['data']['player']['avatar'])
|
||||
else:
|
||||
await self.send_chat_discord(chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png')
|
||||
chat_message = await self.check_if_chat_message(content)
|
||||
if chat_message:
|
||||
info = await self.get_info(chat_message['username'])
|
||||
if info is not None:
|
||||
await self.send_chat_discord(chat_message['username'], chat_message['message'], info['data']['player']['avatar'])
|
||||
else:
|
||||
await self.send_chat_discord(chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png')
|
||||
|
||||
if json.loads(message)['event'] == 'status':
|
||||
current_status = json.loads(message)['args'][0]
|
||||
if await self.config.console_channel() is not None:
|
||||
console = self.bot.get_channel(await self.config.console_channel())
|
||||
if console is not None:
|
||||
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
|
||||
if json.loads(message)['event'] == 'status':
|
||||
current_status = json.loads(message)['args'][0]
|
||||
if await self.config.console_channel() is not None:
|
||||
console = self.bot.get_channel(await self.config.console_channel())
|
||||
if console is not None:
|
||||
await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`")
|
||||
|
||||
def remove_ansi_escape_codes(self, text: str) -> str:
|
||||
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||
|
@ -122,9 +117,9 @@ class Pterodactyl(commands.Cog):
|
|||
regex = await self.config.chat_regex()
|
||||
match: Optional[re.Match[str]] = re.match(regex, text)
|
||||
if match:
|
||||
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(dict))
|
||||
return dict
|
||||
groups = {"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))
|
||||
return groups
|
||||
self.logger.debug("Message is not a chat message")
|
||||
return False
|
||||
|
||||
|
@ -136,9 +131,8 @@ class Pterodactyl(commands.Cog):
|
|||
if response.status == 200:
|
||||
self.logger.debug("Player info retrieved for %s\n%s", username, json.dumps(await response.json()))
|
||||
return await response.json()
|
||||
else:
|
||||
self.logger.error("Failed to retrieve player info for %s: %s", username, response.status)
|
||||
return None
|
||||
self.logger.error("Failed to retrieve player info for %s: %s", username, response.status)
|
||||
return None
|
||||
|
||||
async def send_chat_discord(self, username: str, message: str, avatar_url: str) -> None:
|
||||
self.logger.debug("Sending chat message to Discord")
|
||||
|
@ -170,8 +164,9 @@ class Pterodactyl(commands.Cog):
|
|||
fut.result()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception as e:
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
self.logger.error("WebSocket task has failed: %s", e, exc_info=e)
|
||||
self.task.cancel()
|
||||
self.task = self.get_task()
|
||||
|
||||
async def cog_load(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue