Compare commits

...

3 commits

Author SHA1 Message Date
53adeb4b80
feat(pterodactyl): added configuration commands
All checks were successful
Actions / Lint Code (Ruff) (pull_request) Successful in 7s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 24s
2024-02-28 08:11:23 -05:00
9d4d8b8267
misc(pterodactyl): moved configuration calls into establish_websocket_connection 2024-02-28 08:11:15 -05:00
6e6559d80c
feat(pterodactyl): added more verbose debug logging to establish_websocket_connection 2024-02-28 08:10:46 -05:00

View file

@ -25,10 +25,14 @@ class Pterodactyl(commands.Cog):
self.client = None self.client = None
self.websocket = None self.websocket = None
async def establish_websocket_connection(self, base_url, api_key, server_id): async def establish_websocket_connection(self):
base_url = await self.config.base_url()
api_key = await self.config.api_key()
server_id = await self.config.server_id()
try: try:
client = PterodactylClient(base_url, api_key).client client = PterodactylClient(base_url, api_key).client
websocket_credentials = client.servers.get_websocket(server_id) websocket_credentials = client.servers.get_websocket(server_id)
self.logger.debug("Websocket connection details retrieved: " + websocket_credentials)
except exceptions.ClientConfigError as e: except exceptions.ClientConfigError as e:
self.logger.error(f'Failed to initialize Pterodactyl client: {e}') self.logger.error(f'Failed to initialize Pterodactyl client: {e}')
return return
@ -49,16 +53,49 @@ class Pterodactyl(commands.Cog):
while True: while True:
message = await websocket.recv() message = await websocket.recv()
if json.loads(message)['event'] == 'token expiring': 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) 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']]})
await websocket.send(auth_message)
self.logger.debug("Authentication message sent")
if json.loads(message)['event'] == 'auth success':
self.logger.debug("Authentication successful")
self.logger.debug("Received message: %s", message) self.logger.debug("Received message: %s", message)
async def cog_load(self): async def cog_load(self):
base_url = await self.config.base_url() await self.establish_websocket_connection()
api_key = await self.config.api_key()
server_id = await self.config.server_id()
await self.establish_websocket_connection(base_url, api_key, server_id)
async def cog_unload(self): async def cog_unload(self):
await self.client._session.close() await self.client._session.close()
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"])
async def pterodactyl(self, ctx: commands.Context):
"""Pterodactyl allows you to manage your Pterodactyl Panel from Discord."""
pass
@pterodactyl.group(autohelp = True, name = "config", aliases = ["settings", "set"])
async def pterodactyl_config(self, ctx: commands.Context):
"""Configure Pterodactyl settings."""
pass
@pterodactyl_config.command(name = "url")
async def pterodactyl_config_base_url(self, ctx: commands.Context, base_url: str):
"""Set the base URL of your Pterodactyl Panel. Please include the protocol (http/https)."""
await self.config.base_url.set(base_url)
await ctx.send(f"Base URL set to {base_url}")
await self.establish_websocket_connection()
@pterodactyl_config.command(name = "apikey")
async def pterodactyl_config_api_key(self, ctx: commands.Context, api_key: str):
"""Set the API key for your Pterodactyl Panel."""
await self.config.api_key.set(api_key)
await ctx.send(f"API key set to `{api_key[:5]}...{api_key[-4:]}`")
await self.establish_websocket_connection()
@pterodactyl_config.command(name = "serverid")
async def pterodactyl_config_server_id(self, ctx: commands.Context, server_id: str):
"""Set the server ID for your Pterodactyl Panel."""
await self.config.server_id.set(server_id)
await ctx.send(f"Server ID set to {server_id}")
await self.establish_websocket_connection()