import discord from redbot.core import commands, app_commands, Config class Pterodactyl(commands.Cog): """Pterodactyl allows you to manage your Pterodactyl Panel from Discord.""" def __init__(self, bot): self.bot = bot self.config = Config.get_conf(self, identifier=457581387213637448123567) self.config.register_guild( base_url=None, api_key=None, server_id=None ) async def get_url(self, guild, endpoint = None): """Returns the base url for the Servers API, or the url for a specific endpoint if one is provided.""" if not await self.config.guild(guild).server_id(): raise LookupError("Server ID not set.") elif not await self.config.guild(guild).base_url(): raise LookupError("Base URL not set.") base_url = await self.config.guild(guild).base_url() server_id = await self.config.guild(guild).server_id() url = f"https://{base_url}/api/client/servers/{server_id}/" if endpoint: url += endpoint return url @app_commands.command() async def test(self, interaction: discord.Interaction, endpoint: str = None): """This does stuff!""" try: if endpoint: url = await self.get_url(interaction.guild, endpoint) else: url = await self.get_url(interaction.guild) except LookupError as e: await interaction.response.send_message(f"Something went wrong.\nError: `{e}`", ephemeral=True) return await interaction.response.send_message(url, ephemeral=True) @app_commands.command() async def config(self, interaction: discord.Interaction): """Configures the Pterodactyl cog.""" await interaction.response.send_modal(self.ConfigModal(self.config)) class ConfigModal(discord.ui.Modal, title="Pterodactyl Manager Configuration"): def __init__(self, config): super().__init__() self.config = config base_url = discord.ui.TextInput( label="Base URL", placeholder="Input your Pterodactyl Panel's Base URL here, without HTTPS or HTTP.", style=discord.TextStyle.paragraph, required=False, max_length=300 ) api_key = discord.ui.TextInput( label="API Key", placeholder="Input your Pterodactyl Client API Key here.", style=discord.TextStyle.short, required=False, max_length=300 ) server_id = discord.ui.TextInput( label="Server ID", placeholder="Input your Pterodactyl server's Server ID here.", style=discord.TextStyle.short, required=False, max_length=300 ) async def on_submit(self, interaction: discord.Interaction): message = "" if self.base_url.value != "": self.config.guild(interaction.guild).base_url.set(self.base_url.value) message += f"Base URL set to `{self.base_url.value}`.\n" if self.api_key.value != "": self.config.guild(interaction.guild).api_key.set(self.api_key.value) message += f"API Key set to `{self.api_key.value}`.\n" if self.server_id.value != "": self.config.guild(interaction.guild).server_id.set(self.server_id.value) message += f"Server ID set to `{self.server_id.value}`.\n" if message == "": message = f"No changes were made.\nCurrent configuration:\n- Base URL: `{self.config.guild(interaction.guild).base_url()}`\n- API Key: `{self.config.guild(interaction.guild).api_key()}`\n- Server ID: `{self.config.guild(interaction.guild).server_id()}`" await interaction.response.send_message(message, ephemeral=True)