diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index 55e4a0d..c9c5a73 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -1,6 +1,7 @@ import json import logging +import discord import websockets from pydactyl import PterodactylClient, exceptions from redbot.core import Config, commands @@ -17,6 +18,7 @@ class Pterodactyl(commands.Cog): base_url=None, api_key=None, server_id=None, + console_channel=None, startup_jar=None, startup_arguments=None, power_action_in_progress=False @@ -66,9 +68,14 @@ class Pterodactyl(commands.Cog): 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) + + if json.loads(message)['event'] == 'console output' and await self.config.console_channel() is not None: + channel = self.bot.get_channel(await self.config.console_channel) + await channel.send(json.loads(message)['args'][0]) + #FIXME - Add pagification for long messages to prevent Discord API errors except websockets.exceptions.ConnectionClosed as e: self.logger.debug("WebSocket connection closed: %s", e) websocket_credentials = client.servers.get_websocket(server_id) @@ -84,6 +91,11 @@ class Pterodactyl(commands.Cog): self.task.cancel() await self.client._session.close() # pylint: disable=protected-access + @commands.cog.listener() + async def on_message(self, message: discord.Message): + if message.channel.id == await self.config.console_channel(): + self.websocket.send(json.dumps({"event": "send command", "args": [message.content]})) + @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."""