2023-07-14 11:29:06 -04:00
import discord
from redbot . core import commands , app_commands , Config
2023-07-14 11:04:58 -04:00
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 )
2023-07-14 11:08:49 -04:00
self . config . register_guild (
2023-07-14 11:17:38 -04:00
base_url = None ,
2023-07-14 11:04:58 -04:00
api_key = None ,
2023-07-14 11:16:03 -04:00
server_id = None
2023-07-14 11:04:58 -04:00
)
2023-07-14 11:29:06 -04:00
async def get_url ( self , guild , endpoint = None ) :
2023-07-14 11:04:58 -04:00
""" Returns the base url for the Servers API, or the url for a specific endpoint if one is provided. """
2023-07-14 11:29:06 -04:00
if not await self . config . guild ( guild ) . server_id ( ) :
2023-07-14 11:04:58 -04:00
raise LookupError ( " Server ID not set. " )
2023-07-14 11:29:06 -04:00
elif not await self . config . guild ( guild ) . base_url ( ) :
2023-07-14 11:16:03 -04:00
raise LookupError ( " Base URL not set. " )
2023-07-14 11:29:06 -04:00
base_url = await self . config . guild ( guild ) . base_url ( )
server_id = await self . config . guild ( guild ) . server_id ( )
2023-07-14 11:04:58 -04:00
url = f " https:// { base_url } /api/client/servers/ { server_id } / "
if endpoint :
url + = endpoint
return url
2023-07-14 11:29:06 -04:00
@app_commands.command ( )
async def test ( self , interaction : discord . Interaction , endpoint : str = None ) :
2023-07-14 11:04:58 -04:00
""" This does stuff! """
2023-07-14 11:16:03 -04:00
try :
if endpoint :
2023-07-14 11:29:06 -04:00
url = await self . get_url ( interaction . guild , endpoint )
2023-07-14 11:16:03 -04:00
else :
2023-07-14 11:29:06 -04:00
url = await self . get_url ( interaction . guild )
2023-07-14 11:16:03 -04:00
except LookupError as e :
2023-07-14 11:29:06 -04:00
await interaction . response . send_message ( f " Something went wrong. \n Error: ` { e } ` " , ephemeral = True )
2023-07-14 11:16:58 -04:00
return
2023-07-14 11:29:06 -04:00
await interaction . response . send_message ( url , ephemeral = True )
2023-07-14 11:51:46 -04:00
@app_commands.command ( )
async def config ( self , interaction : discord . Interaction ) :
""" Configures the Pterodactyl cog. """
2023-07-14 12:27:03 -04:00
await interaction . response . send_modal ( self . ConfigModal ( self . config ) )
2023-07-14 11:51:46 -04:00
2023-07-14 12:12:56 -04:00
class ConfigModal ( discord . ui . Modal , title = " Pterodactyl Manager Configuration " ) :
2023-07-14 12:27:03 -04:00
def __init__ ( self , config ) :
2023-07-14 12:12:56 -04:00
super ( ) . __init__ ( )
2023-07-14 12:22:28 -04:00
self . config = config
2023-07-14 12:12:56 -04:00
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
)
2023-07-14 11:51:46 -04:00
2023-07-14 12:12:56 -04:00
async def on_submit ( self , interaction : discord . Interaction ) :
message = " "
if self . base_url . value != " " :
2023-07-14 12:27:03 -04:00
self . config . guild ( interaction . guild ) . base_url . set ( self . base_url )
2023-07-14 12:12:56 -04:00
message + = f " Base URL set to ` { self . base_url . value } `. \n "
if self . api_key . value != " " :
2023-07-14 12:27:03 -04:00
self . config . guild ( interaction . guild ) . api_key . set ( self . api_key )
2023-07-14 12:12:56 -04:00
message + = f " API Key set to ` { self . api_key . value } `. \n "
if self . server_id . value != " " :
2023-07-14 12:27:03 -04:00
self . config . guild ( interaction . guild ) . server_id . set ( self . server_id )
2023-07-14 12:12:56 -04:00
message + = f " Server ID set to ` { self . server_id . value } `. \n "
if message == " " :
2023-07-14 12:27:03 -04:00
message = f " No changes were made. \n Current 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 ( ) } ` "
2023-07-14 12:12:56 -04:00
await interaction . response . send_message ( message , ephemeral = True )