2024-02-29 21:35:09 -05:00
|
|
|
import asyncio
|
2024-02-27 23:02:03 -05:00
|
|
|
import json
|
2024-02-29 23:31:30 -05:00
|
|
|
from typing import Optional
|
|
|
|
|
2024-02-28 12:43:26 -05:00
|
|
|
import discord
|
2024-02-27 23:02:03 -05:00
|
|
|
import websockets
|
2024-02-29 23:31:30 -05:00
|
|
|
from pydactyl import PterodactylClient
|
2024-02-29 23:26:24 -05:00
|
|
|
from redbot.core import commands
|
2024-02-27 23:02:03 -05:00
|
|
|
from redbot.core.bot import Red
|
2024-02-29 23:26:24 -05:00
|
|
|
from redbot.core.utils.chat_formatting import box
|
|
|
|
|
2024-02-29 23:55:51 -05:00
|
|
|
from pterodactyl.config import config, register_config
|
2024-02-29 23:26:24 -05:00
|
|
|
from pterodactyl.logger import logger
|
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-29 23:31:30 -05:00
|
|
|
self.client: Optional[PterodactylClient] = None
|
|
|
|
self.task: Optional[asyncio.Task] = None
|
|
|
|
self.websocket: Optional[websockets.WebSocketClientProtocol] = None
|
2024-02-29 23:55:51 -05:00
|
|
|
register_config(config)
|
2024-02-27 23:02:03 -05:00
|
|
|
|
2024-02-29 23:26:24 -05:00
|
|
|
async def cog_load(self) -> None:
|
|
|
|
self.task = self.get_task()
|
2024-02-29 16:47:20 -05:00
|
|
|
|
2024-02-29 23:26:24 -05:00
|
|
|
async def cog_unload(self) -> None:
|
|
|
|
self.task.cancel()
|
|
|
|
await self.client._session.close() # pylint: disable=protected-access
|
2024-02-29 18:50:22 -05:00
|
|
|
|
2024-02-29 23:26:24 -05:00
|
|
|
def get_task(self) -> asyncio.Task:
|
2024-02-29 23:33:00 -05:00
|
|
|
from pterodactyl.websocket import establish_websocket_connection
|
2024-02-29 23:26:24 -05:00
|
|
|
task = self.bot.loop.create_task(establish_websocket_connection(self), name="Pterodactyl Websocket Connection")
|
2024-02-29 21:35:09 -05:00
|
|
|
task.add_done_callback(self.error_callback)
|
|
|
|
return task
|
|
|
|
|
2024-02-29 23:26:24 -05:00
|
|
|
def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out
|
2024-02-29 21:35:09 -05:00
|
|
|
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 23:26:24 -05:00
|
|
|
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-28 12:44:54 -05:00
|
|
|
@commands.Cog.listener()
|
2024-02-29 23:26:24 -05:00
|
|
|
async def on_message_without_command(self, message: discord.Message) -> None:
|
|
|
|
if message.channel.id == await config.console_channel() and message.author.bot is False:
|
|
|
|
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:
|
2024-02-29 23:26:24 -05:00
|
|
|
logger.error("WebSocket connection closed: %s", e)
|
2024-02-29 20:59:47 -05:00
|
|
|
self.task.cancel()
|
|
|
|
self.task = self.get_task()
|
2024-02-29 23:26:24 -05:00
|
|
|
if message.channel.id == await config.chat_channel() and message.author.bot is False:
|
|
|
|
logger.debug("Received chat message from %s: %s", message.author.id, message.content)
|
|
|
|
channel = self.bot.get_channel(await config.console_channel())
|
2024-02-29 17:27:36 -05:00
|
|
|
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 23:26:24 -05:00
|
|
|
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:
|
2024-02-29 23:26:24 -05:00
|
|
|
logger.error("WebSocket connection closed: %s", e)
|
2024-02-29 20:59:47 -05:00
|
|
|
self.task.cancel()
|
|
|
|
self.task = self.get_task()
|
2024-02-28 12:43:26 -05:00
|
|
|
|
2024-02-29 23:26:24 -05:00
|
|
|
async def get_chat_command(self, username: str, message: str, color: discord.Color) -> str:
|
|
|
|
command: str = await config.chat_command()
|
|
|
|
command = command.replace(".$U", username).replace(".$M", message).replace(".$C", str(color))
|
|
|
|
return command
|
|
|
|
|
2024-02-28 08:11:23 -05:00
|
|
|
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"])
|
2024-02-29 23:26:24 -05:00
|
|
|
async def pterodactyl(self, ctx: commands.Context) -> None:
|
2024-02-28 08:11:23 -05:00
|
|
|
"""Pterodactyl allows you to manage your Pterodactyl Panel from Discord."""
|
|
|
|
|
|
|
|
@pterodactyl.group(autohelp = True, name = "config", aliases = ["settings", "set"])
|
2024-02-29 23:26:24 -05:00
|
|
|
async def pterodactyl_config(self, ctx: commands.Context) -> None:
|
2024-02-28 08:11:23 -05:00
|
|
|
"""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:
|
2024-02-29 23:26:24 -05:00
|
|
|
base_url = await config.base_url()
|
2024-02-29 20:28:26 -05:00
|
|
|
return await ctx.send(f"Base URL is currently set to {base_url}")
|
2024-02-29 23:26:24 -05:00
|
|
|
await config.base_url.set(base_url)
|
2024-02-28 08:11:23 -05:00
|
|
|
await ctx.send(f"Base URL set to {base_url}")
|
2024-02-29 23:26:24 -05:00
|
|
|
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."""
|
2024-02-29 23:26:24 -05:00
|
|
|
await config.api_key.set(api_key)
|
2024-02-28 08:11:23 -05:00
|
|
|
await ctx.send(f"API key set to `{api_key[:5]}...{api_key[-4:]}`")
|
2024-02-29 23:26:24 -05:00
|
|
|
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."""
|
2024-02-29 23:26:24 -05:00
|
|
|
await config.server_id.set(server_id)
|
2024-02-28 08:11:23 -05:00
|
|
|
await ctx.send(f"Server ID set to {server_id}")
|
2024-02-29 23:26:24 -05:00
|
|
|
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."""
|
2024-02-29 23:26:24 -05:00
|
|
|
await config.console_channel.set(channel.id)
|
2024-02-28 12:44:12 -05:00
|
|
|
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."""
|
2024-02-29 23:26:24 -05:00
|
|
|
await config.chat_channel.set(channel.id)
|
2024-02-29 16:47:20 -05:00
|
|
|
await ctx.send(f"Chat channel set to {channel.mention}")
|
2024-02-29 20:28:26 -05:00
|
|
|
|
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:
|
2024-02-29 23:26:24 -05:00
|
|
|
command = await config.chat_command()
|
2024-02-29 22:41:06 -05:00
|
|
|
return await ctx.send(f"Chat command is currently set to:\n{box(command, 'json')}")
|
2024-02-29 23:26:24 -05:00
|
|
|
await config.chat_command.set(command)
|
2024-02-29 22:41:06 -05:00
|
|
|
await ctx.send(f"Chat command set to:\n{box(command, 'json')}")
|
2024-02-29 23:50:54 -05:00
|
|
|
|
2024-03-01 00:23:00 -05:00
|
|
|
@pterodactyl_config.group(name = "regex")
|
|
|
|
async def pterodactyl_config_regex(self, ctx: commands.Context) -> None:
|
|
|
|
"""Set regex patterns."""
|
|
|
|
|
|
|
|
@pterodactyl_config_regex.command(name = "chat")
|
|
|
|
async def pterodactyl_config_regex_chat(self, ctx: commands.Context, *, regex: str = None) -> None:
|
|
|
|
"""Set the regex pattern to match chat messages on the server.
|
|
|
|
|
|
|
|
See [documentation]() for more information."""
|
|
|
|
#TODO - fix this link
|
|
|
|
if regex is None:
|
|
|
|
regex = await config.chat_regex()
|
|
|
|
return await ctx.send(f"Chat regex is currently set to:\n{box(regex, 'regex')}")
|
|
|
|
await config.chat_regex.set(regex)
|
|
|
|
await ctx.send(f"Chat regex set to:\n{box(regex, 'regex')}")
|
|
|
|
|
|
|
|
@pterodactyl_config_regex.command(name = "server")
|
|
|
|
async def pterodactyl_config_regex_server(self, ctx: commands.Context, *, regex: str = None) -> None:
|
2024-02-29 23:50:54 -05:00
|
|
|
"""Set the regex pattern to match server messages on the server.
|
|
|
|
|
|
|
|
See [documentation]() for more information."""
|
|
|
|
#TODO - fix this link
|
|
|
|
if regex is None:
|
|
|
|
regex = await config.server_regex()
|
|
|
|
return await ctx.send(f"Server regex is currently set to:\n{box(regex, 'regex')}")
|
|
|
|
await config.server_regex.set(regex)
|
|
|
|
await ctx.send(f"Server regex set to:\n{box(regex, 'regex')}")
|
2024-03-01 00:05:06 -05:00
|
|
|
|
|
|
|
@pterodactyl_config.group(name = "messages", aliases = ['msg', 'msgs', 'message'])
|
|
|
|
async def pterodactyl_config_messages(self, ctx: commands.Context):
|
|
|
|
"""Configure message settings."""
|
|
|
|
|
|
|
|
@pterodactyl_config_messages.command(name = "startup")
|
|
|
|
async def pterodactyl_config_messages_startup(self, ctx: commands.Context, *, message: str = None) -> None:
|
|
|
|
"""Set the message that will be sent when the server starts."""
|
|
|
|
if message is None:
|
|
|
|
message = await config.startup_msg()
|
|
|
|
return await ctx.send(f"Startup message is currently set to: {message}")
|
|
|
|
await config.startup_msg.set(message)
|
|
|
|
await ctx.send(f"Startup message set to: {message}")
|
|
|
|
|
|
|
|
@pterodactyl_config_messages.command(name = "shutdown")
|
|
|
|
async def pterodactyl_config_messages_shutdown(self, ctx: commands.Context, *, message: str = None) -> None:
|
|
|
|
"""Set the message that will be sent when the server stops."""
|
|
|
|
if message is None:
|
|
|
|
message = await config.shutdown_msg()
|
|
|
|
return await ctx.send(f"Shutdown message is currently set to: {message}")
|
|
|
|
await config.shutdown_msg.set(message)
|
|
|
|
await ctx.send(f"Shutdown message set to: {message}")
|
2024-03-01 00:23:00 -05:00
|
|
|
|
|
|
|
@pterodactyl_config_messages.command(name = "join")
|
|
|
|
async def pterodactyl_config_messages_join(self, ctx: commands.Context, *, message: str = None) -> None:
|
|
|
|
"""Set the message that will be sent when a user joins the server. This is only shown in embeds."""
|
|
|
|
if message is None:
|
|
|
|
message = await config.join_msg()
|
|
|
|
return await ctx.send(f"Join message is currently set to: {message}")
|
|
|
|
await config.join_msg.set(message)
|
|
|
|
await ctx.send(f"Join message set to: {message}")
|
|
|
|
|
|
|
|
@pterodactyl_config_messages.command(name = "leave")
|
|
|
|
async def pterodactyl_config_messages_leave(self, ctx: commands.Context, *, message: str = None) -> None:
|
|
|
|
"""Set the message that will be sent when a user leaves the server. This is only shown in embeds."""
|
|
|
|
if message is None:
|
|
|
|
message = await config.leave_msg()
|
|
|
|
return await ctx.send(f"Leave message is currently set to: {message}")
|
|
|
|
await config.leave_msg.set(message)
|
|
|
|
await ctx.send(f"Leave message set to: {message}")
|