2024-02-29 21:35:09 -05:00
|
|
|
import asyncio
|
2024-02-27 23:02:03 -05:00
|
|
|
import json
|
|
|
|
import logging
|
2024-02-28 13:21:39 -05:00
|
|
|
import re
|
2024-02-29 16:47:20 -05:00
|
|
|
from typing import Optional, Union
|
2024-02-27 23:02:03 -05:00
|
|
|
|
2024-02-29 16:47:20 -05:00
|
|
|
import aiohttp
|
2024-02-28 12:43:26 -05:00
|
|
|
import discord
|
2024-02-27 23:02:03 -05:00
|
|
|
import websockets
|
|
|
|
from pydactyl import PterodactylClient, exceptions
|
|
|
|
from redbot.core import Config, commands
|
|
|
|
from redbot.core.bot import Red
|
2024-02-29 20:28:26 -05:00
|
|
|
from redbot.core.utils.chat_formatting import box, pagify
|
2024-02-27 23:02:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Pterodactyl(commands.Cog):
|
|
|
|
"""Pterodactyl allows you to manage your Pterodactyl Panel from Discord."""
|
|
|
|
|
|
|
|
def __init__(self, bot: Red):
|
|
|
|
self.bot = bot
|
2024-02-27 23:18:17 -05:00
|
|
|
self.config = Config.get_conf(self, identifier=457581387213637448123567, force_registration=True)
|
2024-02-27 23:02:03 -05:00
|
|
|
self.config.register_global(
|
2024-02-27 23:18:17 -05:00
|
|
|
base_url=None,
|
|
|
|
api_key=None,
|
|
|
|
server_id=None,
|
2024-02-28 12:43:26 -05:00
|
|
|
console_channel=None,
|
2024-02-27 23:02:03 -05:00
|
|
|
startup_jar=None,
|
|
|
|
startup_arguments=None,
|
2024-02-29 16:47:20 -05:00
|
|
|
power_action_in_progress=False,
|
|
|
|
chat_regex=r"\[(\d{2}:\d{2}:\d{2})\sINFO\]:\s<(\w+)>\s(.*)",
|
2024-02-29 22:41:06 -05:00
|
|
|
chat_command='tellraw @a ["",{"text":".$U ","color":".$C"},{"text":" (DISCORD): ","color":"blue"},{"text":".$M","color":"white"}]',
|
2024-02-29 16:47:20 -05:00
|
|
|
api_endpoint="minecraft",
|
|
|
|
chat_channel=None
|
2024-02-27 23:02:03 -05:00
|
|
|
)
|
|
|
|
self.logger = logging.getLogger('red.sea.pterodactyl')
|
|
|
|
self.client = None
|
2024-02-28 08:47:24 -05:00
|
|
|
self.task = None
|
2024-02-27 23:02:03 -05:00
|
|
|
self.websocket = None
|
|
|
|
|
2024-02-29 20:28:26 -05:00
|
|
|
async def establish_websocket_connection(self) -> None:
|
2024-02-28 08:49:02 -05:00
|
|
|
self.logger.debug("Establishing WebSocket connection")
|
2024-02-28 08:11:15 -05:00
|
|
|
base_url = await self.config.base_url()
|
|
|
|
api_key = await self.config.api_key()
|
|
|
|
server_id = await self.config.server_id()
|
2024-02-28 08:14:40 -05:00
|
|
|
|
2024-02-27 23:02:03 -05:00
|
|
|
try:
|
2024-02-28 11:57:39 -05:00
|
|
|
client = PterodactylClient(base_url, api_key, debug=True).client
|
2024-02-28 13:00:11 -05:00
|
|
|
self.client = client
|
2024-02-27 23:02:03 -05:00
|
|
|
websocket_credentials = client.servers.get_websocket(server_id)
|
2024-02-28 13:00:11 -05:00
|
|
|
self.logger.debug("""Websocket connection details retrieved:
|
|
|
|
Socket: %s
|
|
|
|
Token: %s...""",
|
|
|
|
websocket_credentials['data']['socket'],
|
|
|
|
websocket_credentials['data']['token'][:20]
|
|
|
|
)
|
|
|
|
#NOTE - The token is truncated to prevent it from being logged in its entirety, for security reasons
|
2024-02-27 23:02:03 -05:00
|
|
|
except exceptions.ClientConfigError as e:
|
2024-02-29 20:28:26 -05:00
|
|
|
return self.logger.error('Failed to initialize Pterodactyl client: %s', e)
|
2024-02-27 23:02:03 -05:00
|
|
|
except exceptions.PterodactylApiError as e:
|
2024-02-29 20:28:26 -05:00
|
|
|
return self.logger.error('Failed to retrieve Pterodactyl websocket: %s', e)
|
2024-02-27 23:02:03 -05:00
|
|
|
|
2024-02-29 22:07:22 -05:00
|
|
|
async with websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60) as websocket:
|
2024-02-29 22:15:44 -05:00
|
|
|
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")
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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 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)
|
|
|
|
|
|
|
|
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]}`")
|
2024-02-27 23:02:03 -05:00
|
|
|
|
2024-02-29 16:47:20 -05:00
|
|
|
def remove_ansi_escape_codes(self, text: str) -> str:
|
2024-02-28 13:21:39 -05:00
|
|
|
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
|
|
|
#NOTE - https://chat.openai.com/share/d92f9acf-d776-4fd6-a53f-b14ac15dd540
|
|
|
|
return ansi_escape.sub('', text)
|
|
|
|
|
2024-02-29 16:53:09 -05:00
|
|
|
async def check_if_chat_message(self, text: str) -> Union[bool, dict]:
|
2024-02-29 16:52:10 -05:00
|
|
|
self.logger.debug("Checking if message is a chat message")
|
2024-02-29 16:53:09 -05:00
|
|
|
regex = await self.config.chat_regex()
|
2024-02-29 21:15:46 -05:00
|
|
|
match: Optional[re.Match[str]] = re.match(regex, text)
|
2024-02-29 20:45:31 -05:00
|
|
|
if match:
|
2024-02-29 22:15:44 -05:00
|
|
|
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
|
2024-02-29 16:52:10 -05:00
|
|
|
self.logger.debug("Message is not a chat message")
|
2024-02-29 16:47:20 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
async def get_info(self, username: str) -> Optional[dict]:
|
2024-02-29 16:52:10 -05:00
|
|
|
self.logger.debug("Retrieving player info for %s", username)
|
2024-02-29 16:55:45 -05:00
|
|
|
endpoint = await self.config.api_endpoint()
|
2024-02-29 16:47:20 -05:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
async with session.get(f"https://playerdb.co/api/player/{endpoint}/{username}") as response:
|
|
|
|
if response.status == 200:
|
2024-02-29 16:58:19 -05:00
|
|
|
self.logger.debug("Player info retrieved for %s\n%s", username, json.dumps(await response.json()))
|
2024-02-29 16:47:20 -05:00
|
|
|
return await response.json()
|
2024-02-29 22:15:44 -05:00
|
|
|
self.logger.error("Failed to retrieve player info for %s: %s", username, response.status)
|
|
|
|
return None
|
2024-02-29 16:47:20 -05:00
|
|
|
|
|
|
|
async def send_chat_discord(self, username: str, message: str, avatar_url: str) -> None:
|
2024-02-29 16:52:10 -05:00
|
|
|
self.logger.debug("Sending chat message to Discord")
|
2024-02-29 16:47:20 -05:00
|
|
|
channel = self.bot.get_channel(await self.config.chat_channel())
|
|
|
|
if channel is not None:
|
|
|
|
webhooks = await channel.webhooks()
|
|
|
|
webhook = discord.utils.get(webhooks, name="Pterodactyl Chat")
|
|
|
|
if webhook is None:
|
|
|
|
webhook = await channel.create_webhook(name="Pterodactyl Chat")
|
|
|
|
await webhook.send(content=message, username=username, avatar_url=avatar_url)
|
2024-02-29 16:52:10 -05:00
|
|
|
self.logger.debug("Chat message sent to Discord")
|
2024-02-29 17:14:06 -05:00
|
|
|
else:
|
|
|
|
self.logger.debug("Chat channel not set. Skipping sending chat message to Discord")
|
2024-02-29 16:47:20 -05:00
|
|
|
|
2024-02-29 22:41:06 -05:00
|
|
|
async def get_chat_command(self, username: str, message: str, color: discord.Color):
|
|
|
|
command = await self.config.chat_command()
|
|
|
|
command = command.replace(".$U", username).replace(".$M", message).replace(".$C", str(color))
|
|
|
|
return command
|
2024-02-29 18:50:22 -05:00
|
|
|
|
2024-02-28 11:54:16 -05:00
|
|
|
def get_task(self):
|
2024-02-29 21:35:09 -05:00
|
|
|
task = self.bot.loop.create_task(self.establish_websocket_connection(), name="Pterodactyl Websocket Connection")
|
|
|
|
task.add_done_callback(self.error_callback)
|
|
|
|
return task
|
|
|
|
|
|
|
|
def error_callback(self, fut): #NOTE - Thanks flame442 and zephyrkul for helping me figure this out
|
|
|
|
try:
|
|
|
|
fut.result()
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
pass
|
2024-02-29 22:15:44 -05:00
|
|
|
except Exception as e: # pylint: disable=broad-exception-caught
|
2024-02-29 21:35:09 -05:00
|
|
|
self.logger.error("WebSocket task has failed: %s", e, exc_info=e)
|
2024-02-29 22:06:59 -05:00
|
|
|
self.task.cancel()
|
2024-02-29 21:38:54 -05:00
|
|
|
self.task = self.get_task()
|
2024-02-28 08:47:24 -05:00
|
|
|
|
2024-02-27 23:02:03 -05:00
|
|
|
async def cog_load(self):
|
2024-02-28 11:54:16 -05:00
|
|
|
self.task = self.get_task()
|
2024-02-27 23:02:03 -05:00
|
|
|
|
|
|
|
async def cog_unload(self):
|
2024-02-28 11:08:16 -05:00
|
|
|
self.task.cancel()
|
|
|
|
await self.client._session.close() # pylint: disable=protected-access
|
2024-02-28 08:11:23 -05:00
|
|
|
|
2024-02-28 12:44:54 -05:00
|
|
|
@commands.Cog.listener()
|
2024-02-29 21:51:18 -05:00
|
|
|
async def on_message_without_command(self, message: discord.Message):
|
2024-02-29 21:00:35 -05:00
|
|
|
if message.channel.id == await self.config.console_channel() and message.author.bot is False:
|
2024-02-29 18:39:01 -05:00
|
|
|
self.logger.debug("Received console command from %s: %s", message.author.id, message.content)
|
2024-02-29 17:27:36 -05:00
|
|
|
await message.channel.send(f"Received console command from {message.author.id}: {message.content[:1900]}")
|
2024-02-29 20:59:47 -05:00
|
|
|
try:
|
|
|
|
await self.websocket.send(json.dumps({"event": "send command", "args": [message.content]}))
|
|
|
|
except websockets.exceptions.ConnectionClosed as e:
|
|
|
|
self.logger.error("WebSocket connection closed: %s", e)
|
|
|
|
self.task.cancel()
|
|
|
|
self.task = self.get_task()
|
2024-02-29 21:00:35 -05:00
|
|
|
if message.channel.id == await self.config.chat_channel() and message.author.bot is False:
|
2024-02-29 18:39:01 -05:00
|
|
|
self.logger.debug("Received chat message from %s: %s", message.author.id, message.content)
|
2024-02-29 17:27:36 -05:00
|
|
|
channel = self.bot.get_channel(await self.config.console_channel())
|
|
|
|
if channel:
|
|
|
|
await channel.send(f"Received chat message from {message.author.id}: {message.content[:1900]}")
|
2024-02-29 22:41:06 -05:00
|
|
|
msg = json.dumps({"event": "send command", "args": [await self.get_chat_command(message.author.display_name, message.content, message.author.color)]})
|
2024-02-29 18:39:01 -05:00
|
|
|
self.logger.debug("Sending chat message to server:\n%s", msg)
|
2024-02-29 20:59:47 -05:00
|
|
|
try:
|
|
|
|
await self.websocket.send(msg)
|
|
|
|
except websockets.exceptions.ConnectionClosed as e:
|
|
|
|
self.logger.error("WebSocket connection closed: %s", e)
|
|
|
|
self.task.cancel()
|
|
|
|
self.task = self.get_task()
|
2024-02-28 12:43:26 -05:00
|
|
|
|
2024-02-28 08:11:23 -05:00
|
|
|
@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."""
|
|
|
|
|
|
|
|
@pterodactyl.group(autohelp = True, name = "config", aliases = ["settings", "set"])
|
|
|
|
async def pterodactyl_config(self, ctx: commands.Context):
|
|
|
|
"""Configure Pterodactyl settings."""
|
|
|
|
|
|
|
|
@pterodactyl_config.command(name = "url")
|
2024-02-29 20:28:26 -05:00
|
|
|
async def pterodactyl_config_base_url(self, ctx: commands.Context, *, base_url: str = None) -> None:
|
2024-02-28 08:11:23 -05:00
|
|
|
"""Set the base URL of your Pterodactyl Panel. Please include the protocol (http/https)."""
|
2024-02-29 20:28:26 -05:00
|
|
|
if base_url is None:
|
|
|
|
base_url = await self.config.base_url()
|
|
|
|
return await ctx.send(f"Base URL is currently set to {base_url}")
|
2024-02-28 08:11:23 -05:00
|
|
|
await self.config.base_url.set(base_url)
|
|
|
|
await ctx.send(f"Base URL set to {base_url}")
|
2024-02-29 20:28:26 -05:00
|
|
|
self.logger.info("Configuration value set: base_url = %s\nRestarting task...", base_url)
|
2024-02-28 10:46:42 -05:00
|
|
|
self.task.cancel()
|
2024-02-28 11:54:16 -05:00
|
|
|
self.task = self.get_task()
|
2024-02-28 08:11:23 -05:00
|
|
|
|
|
|
|
@pterodactyl_config.command(name = "apikey")
|
2024-02-29 20:28:26 -05:00
|
|
|
async def pterodactyl_config_api_key(self, ctx: commands.Context, *, api_key: str) -> None:
|
2024-02-28 08:11:23 -05:00
|
|
|
"""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:]}`")
|
2024-02-29 20:28:26 -05:00
|
|
|
self.logger.info("Configuration value set: api_key = %s\nRestarting task...", api_key)
|
2024-02-28 10:46:42 -05:00
|
|
|
self.task.cancel()
|
2024-02-28 11:54:16 -05:00
|
|
|
self.task = self.get_task()
|
2024-02-28 08:11:23 -05:00
|
|
|
|
|
|
|
@pterodactyl_config.command(name = "serverid")
|
2024-02-29 20:28:26 -05:00
|
|
|
async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None:
|
2024-02-28 08:11:23 -05:00
|
|
|
"""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}")
|
2024-02-29 20:28:26 -05:00
|
|
|
self.logger.info("Configuration value set: server_id = %s\nRestarting task...", server_id)
|
2024-02-28 10:46:42 -05:00
|
|
|
self.task.cancel()
|
2024-02-28 11:54:16 -05:00
|
|
|
self.task = self.get_task()
|
2024-02-28 12:44:12 -05:00
|
|
|
|
|
|
|
@pterodactyl_config.command(name = "consolechannel")
|
2024-02-29 20:28:26 -05:00
|
|
|
async def pterodactyl_config_console_channel(self, ctx: commands.Context, channel: discord.TextChannel) -> None:
|
2024-02-28 12:44:12 -05:00
|
|
|
"""Set the channel to send console output to."""
|
|
|
|
await self.config.console_channel.set(channel.id)
|
|
|
|
await ctx.send(f"Console channel set to {channel.mention}")
|
2024-02-29 16:47:20 -05:00
|
|
|
|
2024-02-29 20:31:29 -05:00
|
|
|
@pterodactyl_config.group(name = "chat")
|
|
|
|
async def pterodactyl_config_chat(self, ctx: commands.Context):
|
|
|
|
"""Configure chat settings."""
|
|
|
|
|
|
|
|
@pterodactyl_config_chat.command(name = "channel")
|
2024-02-29 20:28:26 -05:00
|
|
|
async def pterodactyl_config_chat_channel(self, ctx: commands.Context, channel: discord.TextChannel) -> None:
|
2024-02-29 16:47:20 -05:00
|
|
|
"""Set the channel to send chat output to."""
|
|
|
|
await self.config.chat_channel.set(channel.id)
|
|
|
|
await ctx.send(f"Chat channel set to {channel.mention}")
|
2024-02-29 20:28:26 -05:00
|
|
|
|
|
|
|
@pterodactyl_config_chat.command(name = "regex")
|
|
|
|
async def pterodactyl_config_chat_regex(self, ctx: commands.Context, *, regex: str = None) -> None:
|
2024-02-29 22:41:06 -05:00
|
|
|
"""Set the regex pattern to match chat messages on the server.
|
2024-02-29 20:28:26 -05:00
|
|
|
|
|
|
|
See [documentation]() for more information."""
|
|
|
|
#TODO - fix this link
|
|
|
|
if regex is None:
|
|
|
|
regex = await self.config.chat_regex()
|
|
|
|
return await ctx.send(f"Chat regex is currently set to:\n{box(regex, 'regex')}")
|
|
|
|
await self.config.chat_regex.set(regex)
|
|
|
|
await ctx.send(f"Chat regex set to:\n{box(regex, 'regex')}")
|
|
|
|
|
2024-02-29 22:41:06 -05:00
|
|
|
@pterodactyl_config_chat.command(name = "command")
|
|
|
|
async def pterodactyl_config_chat_command(self, ctx: commands.Context, *, command: str = None) -> None:
|
|
|
|
"""Set the command that will be used to send messages from Discord.
|
2024-02-29 20:28:26 -05:00
|
|
|
|
|
|
|
Required placeholders: `.$U` (username), `.$M` (message), `.$C` (color)
|
|
|
|
See [documentation]() for more information."""
|
|
|
|
#TODO - fix this link
|
2024-02-29 22:41:06 -05:00
|
|
|
if command is None:
|
|
|
|
command = await self.config.chat_command()
|
|
|
|
return await ctx.send(f"Chat command is currently set to:\n{box(command, 'json')}")
|
|
|
|
await self.config.chat_command.set(command)
|
|
|
|
await ctx.send(f"Chat command set to:\n{box(command, 'json')}")
|