feat(pterodactyl): introduced the cog
This commit is contained in:
parent
57c7bce6cd
commit
6158d05960
5 changed files with 220 additions and 43 deletions
5
pterodactyl/__init__.py
Normal file
5
pterodactyl/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from .pterodactyl import Pterodactyl
|
||||
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(Pterodactyl(bot))
|
19
pterodactyl/info.json
Normal file
19
pterodactyl/info.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"author" : ["SeaswimmerTheFsh (seasw.)"],
|
||||
"install_msg" : "Thank you for installing Pterodactyl!\nYou can find the source code of this cog [here](https://coastalcommits.com/SeaswimmerTheFsh/SeaCogs). Based off of PhasecoreX's [UwU](<https://github.com/PhasecoreX/PCXCogs/tree/master/uwu>) cog.",
|
||||
"name" : "Pterodactyl",
|
||||
"short" : "Interface with Pterodactyl through websockets.",
|
||||
"description" : "Interface with Pterodactyl through websockets.",
|
||||
"end_user_data_statement" : "This cog does not store end user data.",
|
||||
"hidden": false,
|
||||
"disabled": false,
|
||||
"min_bot_version": "3.5.0",
|
||||
"min_python_version": [3, 8, 0],
|
||||
"requirements": ["py-dactyl"],
|
||||
"tags": [
|
||||
"pterodactyl",
|
||||
"minecraft",
|
||||
"server",
|
||||
"management"
|
||||
]
|
||||
}
|
61
pterodactyl/pterodactyl.py
Normal file
61
pterodactyl/pterodactyl.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
import json
|
||||
import logging
|
||||
|
||||
import websockets
|
||||
from pydactyl import PterodactylClient, exceptions
|
||||
from redbot.core import Config, commands
|
||||
from redbot.core.bot import Red
|
||||
|
||||
|
||||
class Pterodactyl(commands.Cog):
|
||||
"""Pterodactyl allows you to manage your Pterodactyl Panel from Discord."""
|
||||
|
||||
def __init__(self, bot: Red):
|
||||
self.bot = bot
|
||||
self.config = Config.get_conf(self, identifier=457581387213637448123567)
|
||||
self.config.register_global(
|
||||
base_url='https://mc.bloom.host',
|
||||
api_key='ptlc_G1leFiGsBXeo4lkRasXOMUJhE1aXnygQmg2AKgLKNjk',
|
||||
server_id='5756a968',
|
||||
startup_jar=None,
|
||||
startup_arguments=None,
|
||||
power_action_in_progress=False
|
||||
)
|
||||
self.logger = logging.getLogger('red.sea.pterodactyl')
|
||||
self.client = None
|
||||
self.websocket = None
|
||||
|
||||
async def establish_websocket_connection(self, base_url, api_key, server_id):
|
||||
try:
|
||||
client = PterodactylClient(base_url, api_key).client
|
||||
websocket_credentials = client.servers.get_websocket(server_id)
|
||||
except exceptions.ClientConfigError as e:
|
||||
self.logger.error(f'Failed to initialize Pterodactyl client: {e}')
|
||||
return
|
||||
except exceptions.PterodactylApiError as e:
|
||||
self.logger.error(f'Failed to retrieve Pterodactyl websocket: {e}')
|
||||
return
|
||||
|
||||
async with websockets.connect(websocket_credentials['data']['socket']) as websocket:
|
||||
self.logger.debug("WebSocket connection established")
|
||||
|
||||
# Send authentication token
|
||||
auth_message = json.dumps({"event": "auth", "args": websocket_credentials['data']['token']})
|
||||
await websocket.send(auth_message)
|
||||
self.logger.debug("Authentication message sent")
|
||||
|
||||
self.client = client
|
||||
self.websocket = websocket
|
||||
|
||||
while True:
|
||||
message = await websocket.recv()
|
||||
self.logger.debug("Received message: %s", message)
|
||||
|
||||
async def cog_load(self):
|
||||
base_url = await self.config.base_url()
|
||||
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):
|
||||
await self.client._session.close()
|
Loading…
Add table
Add a link
Reference in a new issue