Add Pterodactyl cog #19

Merged
cswimr merged 139 commits from pterodactyl into main 2024-03-02 00:07:42 -05:00
Showing only changes of commit 9e63b4e798 - Show all commits

View file

@ -29,54 +29,51 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
logger.debug("Authentication message sent") logger.debug("Authentication message sent")
coginstance.websocket = websocket coginstance.websocket = websocket
current_status = ''
await loop(coginstance, websocket) while True: # pylint: disable=too-many-nested-blocks
message = await websocket.recv()
if json.loads(message)['event'] in ('token expiring', 'token expired'):
logger.debug("Received token expiring/expired event. Refreshing token.")
websocket_credentials = await retrieve_websocket_credentials(coginstance)
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
await websocket.send(auth_message)
logger.debug("Authentication message sent")
async def loop(coginstance: Pterodactyl, websocket: websockets.WebSocketClientProtocol) -> None: if json.loads(message)['event'] == 'auth success':
current_status = '' logger.info("WebSocket authentication successful")
while True: # pylint: disable=too-many-nested-blocks
message = await websocket.recv()
if json.loads(message)['event'] in ('token expiring', 'token expired'):
logger.debug("Received token expiring/expired event. Refreshing token.")
websocket_credentials = await retrieve_websocket_credentials(coginstance)
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
await websocket.send(auth_message)
logger.debug("Authentication message sent")
if json.loads(message)['event'] == 'auth success': if json.loads(message)['event'] == 'console output' and await config.console_channel() is not None:
logger.info("WebSocket authentication successful") if current_status in ('running', 'offline', ''):
content = remove_ansi_escape_codes(json.loads(message)['args'][0])
if json.loads(message)['event'] == 'console output' and await config.console_channel() is not None: channel = coginstance.bot.get_channel(await config.console_channel())
if current_status in ('running', 'offline', ''):
content = remove_ansi_escape_codes(json.loads(message)['args'][0])
channel = coginstance.bot.get_channel(await 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 check_if_chat_message(content)
if chat_message:
info = await get_info(chat_message['username'])
if info is not None:
await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], info['data']['player']['avatar'])
else:
await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png')
server_message = await check_if_server_message(content)
if server_message:
channel = coginstance.bot.get_channel(await config.chat_channel())
if channel is not None: if channel is not None:
await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...') if content.startswith('['):
pagified_content = pagify(content, delims=[" ", "\n"])
for page in pagified_content:
await channel.send(content=page)
if json.loads(message)['event'] == 'status': chat_message = await check_if_chat_message(content)
current_status = json.loads(message)['args'][0] if chat_message:
if await config.console_channel() is not None: info = await get_info(chat_message['username'])
console = coginstance.bot.get_channel(await config.console_channel()) if info is not None:
if console is not None: await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], info['data']['player']['avatar'])
await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`") else:
await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png')
server_message = await check_if_server_message(content)
if server_message:
channel = coginstance.bot.get_channel(await config.chat_channel())
if channel is not None:
await channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...')
if json.loads(message)['event'] == 'status':
current_status = json.loads(message)['args'][0]
if await config.console_channel() is not None:
console = coginstance.bot.get_channel(await config.console_channel())
if console is not None:
await console.send(f"Server status changed! `{json.loads(message)['args'][0]}`")
async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[dict]: async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[dict]:
base_url = await config.base_url() base_url = await config.base_url()