Compare commits
No commits in common. "fd6fca640cc121774817aaaa828972216a880da6" and "99a6a0dfda13a44b394525bed1807b2eac72fede" have entirely different histories.
fd6fca640c
...
99a6a0dfda
1 changed files with 18 additions and 141 deletions
|
@ -20,17 +20,6 @@ class Pterodactyl(commands.Cog):
|
||||||
startup_arguments=None
|
startup_arguments=None
|
||||||
)
|
)
|
||||||
|
|
||||||
async def get_headers(self, guild: discord.Guild):
|
|
||||||
"""Returns the headers used to access the Pterodactyl API."""
|
|
||||||
if await self.config.guild(guild).api_key() is None:
|
|
||||||
raise LookupError("API Key not set.")
|
|
||||||
headers = {
|
|
||||||
"Authorization": f"Bearer {await self.config.guild(guild).api_key()}",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Accept": "application/json"
|
|
||||||
}
|
|
||||||
return headers
|
|
||||||
|
|
||||||
async def get_url(self, guild, endpoint = None):
|
async def get_url(self, guild, endpoint = None):
|
||||||
"""Returns the base url for the server's API, or the url for a specific API endpoint if one is provided."""
|
"""Returns the base url for the server's API, or the url for a specific API endpoint if one is provided."""
|
||||||
if await self.config.guild(guild).server_id() is None:
|
if await self.config.guild(guild).server_id() is None:
|
||||||
|
@ -50,89 +39,7 @@ class Pterodactyl(commands.Cog):
|
||||||
async with session.put(url, headers=headers, json=data) as response:
|
async with session.put(url, headers=headers, json=data) as response:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
power = app_commands.Group(name='power', description="Controls the server's power state.")
|
|
||||||
|
|
||||||
@power.command(name='start', description="Starts the server.")
|
|
||||||
@app_commands.guild_only()
|
|
||||||
async def start(self, interaction: discord.Interaction):
|
|
||||||
"""Starts the server."""
|
|
||||||
await interaction.defer(ephemeral=True, thinking=True)
|
|
||||||
interaction_message = await interaction.original_response()
|
|
||||||
headers = await self.get_headers(interaction.guild)
|
|
||||||
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
|
||||||
requests_json = response.json()
|
|
||||||
current_status = requests_json['attributes']['current_state']
|
|
||||||
if current_status == "offline":
|
|
||||||
passed_info = {
|
|
||||||
"headers": headers,
|
|
||||||
"interaction": interaction,
|
|
||||||
"guild": interaction.guild,
|
|
||||||
"signal": "start",
|
|
||||||
"target_signal": "running",
|
|
||||||
"message": "Server starting...",
|
|
||||||
"completed_message": "Server started!"
|
|
||||||
}
|
|
||||||
await interaction_message.edit(content="Are you sure you'd like to start the server?", view=self.PowerButtons(timeout=180, passed_info=passed_info))
|
|
||||||
else:
|
|
||||||
message = await interaction_message.edit(content="The server is already running!")
|
|
||||||
await message.delete(delay=3)
|
|
||||||
|
|
||||||
@power.command(name='restart', description="Restarts the server.")
|
|
||||||
@app_commands.guild_only()
|
|
||||||
async def restart(self, interaction: discord.Interaction):
|
|
||||||
"""Restarts the server."""
|
|
||||||
await interaction.defer(ephemeral=True, thinking=True)
|
|
||||||
interaction_message = await interaction.original_response()
|
|
||||||
headers = await self.get_headers(interaction.guild)
|
|
||||||
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
|
||||||
requests_json = response.json()
|
|
||||||
current_status = requests_json['attributes']['current_state']
|
|
||||||
if current_status == "running":
|
|
||||||
passed_info = {
|
|
||||||
"headers": headers,
|
|
||||||
"interaction": interaction,
|
|
||||||
"guild": interaction.guild,
|
|
||||||
"signal": "restart",
|
|
||||||
"target_signal": "running",
|
|
||||||
"message": "Server restarting...",
|
|
||||||
"completed_message": "Server restarted!"
|
|
||||||
}
|
|
||||||
await interaction_message.edit(content="Are you sure you'd like to restart the server?", view=self.PowerButtons(timeout=180, passed_info=passed_info))
|
|
||||||
elif current_status == "offline":
|
|
||||||
message = await interaction_message.edit(content="The server is offline!")
|
|
||||||
await message.delete(delay=3)
|
|
||||||
elif current_status == "starting":
|
|
||||||
message = await interaction_message.edit(content="The server is already starting!")
|
|
||||||
await message.delete(delay=3)
|
|
||||||
|
|
||||||
@power.command(name='stop', description="Stops the server.")
|
|
||||||
@app_commands.guild_only()
|
|
||||||
async def stop(self, interaction: discord.Interaction):
|
|
||||||
"""Stops the server."""
|
|
||||||
await interaction.defer(ephemeral=True, thinking=True)
|
|
||||||
interaction_message = await interaction.original_response()
|
|
||||||
headers = await self.get_headers(interaction.guild)
|
|
||||||
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
|
||||||
requests_json = response.json()
|
|
||||||
current_status = requests_json['attributes']['current_state']
|
|
||||||
if current_status == "running" or current_status == "starting":
|
|
||||||
passed_info = {
|
|
||||||
"headers": headers,
|
|
||||||
"interaction": interaction,
|
|
||||||
"guild": interaction.guild,
|
|
||||||
"signal": "stop",
|
|
||||||
"target_signal": "offline",
|
|
||||||
"message": "Server stopping...",
|
|
||||||
"completed_message": "Server stopped!"
|
|
||||||
}
|
|
||||||
await interaction_message.edit(content="Are you sure you'd like to stop the server?", view=self.PowerButtons(timeout=180, passed_info=passed_info))
|
|
||||||
elif current_status == "offline":
|
|
||||||
message = await interaction_message.edit(content="The server is already offline!")
|
|
||||||
await message.delete(delay=3)
|
|
||||||
|
|
||||||
|
|
||||||
@app_commands.command(description="Updates the server.")
|
@app_commands.command(description="Updates the server.")
|
||||||
@app_commands.guild_only()
|
|
||||||
async def update(self, interaction: discord.Interaction):
|
async def update(self, interaction: discord.Interaction):
|
||||||
"""Updates the server using the arguments provided in the server's configuration."""
|
"""Updates the server using the arguments provided in the server's configuration."""
|
||||||
await interaction.response.defer(ephemeral=True, thinking=True)
|
await interaction.response.defer(ephemeral=True, thinking=True)
|
||||||
|
@ -144,13 +51,17 @@ class Pterodactyl(commands.Cog):
|
||||||
await interaction_message.edit(f"Something went wrong.\nError: `Startup jar not set.`", ephemeral=True)
|
await interaction_message.edit(f"Something went wrong.\nError: `Startup jar not set.`", ephemeral=True)
|
||||||
raise LookupError("Startup jar not set.")
|
raise LookupError("Startup jar not set.")
|
||||||
elif await self.config.guild(interaction.guild).startup_arguments() is None:
|
elif await self.config.guild(interaction.guild).startup_arguments() is None:
|
||||||
await interaction_message.edit(f"Something went wrong.\nError: `Startup arguments not set.`", ephemeral=True)
|
await interaction.response.send_message(f"Something went wrong.\nError: `Startup arguments not set.`", ephemeral=True)
|
||||||
raise LookupError("Startup arguments not set.")
|
raise LookupError("Startup arguments not set.")
|
||||||
else:
|
else:
|
||||||
api_key = await self.config.guild(interaction.guild).api_key()
|
api_key = await self.config.guild(interaction.guild).api_key()
|
||||||
startup_jar = await self.config.guild(interaction.guild).startup_jar()
|
startup_jar = await self.config.guild(interaction.guild).startup_jar()
|
||||||
startup_commands = await self.config.guild(interaction.guild).startup_arguments()
|
startup_commands = await self.config.guild(interaction.guild).startup_arguments()
|
||||||
headers = await self.get_headers(interaction.guild)
|
headers = {
|
||||||
|
"Authorization": f"Bearer {api_key}",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Accept": "application/json"
|
||||||
|
}
|
||||||
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
||||||
response_dict = response.json()
|
response_dict = response.json()
|
||||||
list_var = requests.get(await self.get_url(interaction.guild, "startup"), headers=headers)
|
list_var = requests.get(await self.get_url(interaction.guild, "startup"), headers=headers)
|
||||||
|
@ -212,12 +123,11 @@ class Pterodactyl(commands.Cog):
|
||||||
|
|
||||||
@ui.button(label="Yes", style=discord.ButtonStyle.success)
|
@ui.button(label="Yes", style=discord.ButtonStyle.success)
|
||||||
async def yes_button(self, button:ui.Button, interaction:discord.Interaction):
|
async def yes_button(self, button:ui.Button, interaction:discord.Interaction):
|
||||||
headers = self.passed_info['headers']
|
requests.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=self.passed_info['headers'], json={"signal": "stop"})
|
||||||
requests.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=headers, json={"signal": "stop"})
|
|
||||||
await self.passed_info['interaction'].edit_original_response(content="Server stopping...", view=None)
|
await self.passed_info['interaction'].edit_original_response(content="Server stopping...", view=None)
|
||||||
while True:
|
while True:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=headers) as response:
|
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=self.passed_info['headers']) as response:
|
||||||
response_dict = await response.json()
|
response_dict = await response.json()
|
||||||
if response_dict['attributes']['current_state'] == "offline":
|
if response_dict['attributes']['current_state'] == "offline":
|
||||||
await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!")
|
await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!")
|
||||||
|
@ -226,13 +136,13 @@ class Pterodactyl(commands.Cog):
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
continue
|
continue
|
||||||
for data in self.passed_info['updater_startup_vars']:
|
for data in self.passed_info['updater_startup_vars']:
|
||||||
await Pterodactyl.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=headers, data=data)
|
await Pterodactyl.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data)
|
||||||
requests.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=headers, json={"signal": "start"})
|
requests.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=self.passed_info['headers'], json={"signal": "start"})
|
||||||
await self.passed_info['interaction'].edit_original_response(content="Updater started...")
|
await self.passed_info['interaction'].edit_original_response(content="Updater started...")
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
while True:
|
while True:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=headers) as response:
|
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=self.passed_info['headers']) as response:
|
||||||
response_dict = await response.json()
|
response_dict = await response.json()
|
||||||
if response_dict['attributes']['current_state'] == "offline":
|
if response_dict['attributes']['current_state'] == "offline":
|
||||||
await self.passed_info['interaction'].edit_original_response(content="Updater finished!")
|
await self.passed_info['interaction'].edit_original_response(content="Updater finished!")
|
||||||
|
@ -241,13 +151,13 @@ class Pterodactyl(commands.Cog):
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
continue
|
continue
|
||||||
for data in self.passed_info['old_startup_vars']:
|
for data in self.passed_info['old_startup_vars']:
|
||||||
await Pterodactyl.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers, data)
|
await Pterodactyl.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), self.passed_info['headers'], data)
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
requests.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=headers, json={"signal": "start"})
|
requests.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=self.passed_info['headers'], json={"signal": "start"})
|
||||||
await self.passed_info['interaction'].edit_original_response(content="Server starting...")
|
await self.passed_info['interaction'].edit_original_response(content="Server starting...")
|
||||||
while True:
|
while True:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=headers) as response:
|
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=self.passed_info['headers']) as response:
|
||||||
response_dict = await response.json()
|
response_dict = await response.json()
|
||||||
if response_dict['attributes']['current_state'] == "running":
|
if response_dict['attributes']['current_state'] == "running":
|
||||||
await self.passed_info['interaction'].edit_original_response(content="Server started!\nUpdate process completed!")
|
await self.passed_info['interaction'].edit_original_response(content="Server started!\nUpdate process completed!")
|
||||||
|
@ -261,40 +171,9 @@ class Pterodactyl(commands.Cog):
|
||||||
message = await self.passed_info['interaction'].edit_original_response(content=f"Command cancelled.", view=None)
|
message = await self.passed_info['interaction'].edit_original_response(content=f"Command cancelled.", view=None)
|
||||||
await message.delete(delay=3)
|
await message.delete(delay=3)
|
||||||
|
|
||||||
class PowerButtons(ui.View):
|
@app_commands.command()
|
||||||
def __init__(self, timeout, passed_info):
|
async def test(self, interaction: discord.Interaction, endpoint: str = None):
|
||||||
super().__init__()
|
"""This does stuff!"""
|
||||||
self.passed_info = passed_info
|
|
||||||
self.config = Config.get_conf(None, cog_name='Pterodactyl', identifier=457581387213637448123567)
|
|
||||||
|
|
||||||
@ui.button(label="Yes", style=discord.ButtonStyle.success)
|
|
||||||
async def yes_button(self, button:ui.Button, interaction:discord.Interaction):
|
|
||||||
headers = self.passed_info['headers']
|
|
||||||
requests.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=headers, json={"signal": self.passed_info['signal']})
|
|
||||||
message = await self.passed_info['interaction'].edit_original_response(content=self.passed_info['message'], view=None)
|
|
||||||
while True:
|
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=headers) as response:
|
|
||||||
response_dict = await response.json()
|
|
||||||
if response_dict['attributes']['current_state'] == self.passed_info['target_signal']:
|
|
||||||
await message.edit(content=self.passed_info['completed_message'])
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
await asyncio.sleep(1)
|
|
||||||
continue
|
|
||||||
|
|
||||||
@ui.button(label="No", style=discord.ButtonStyle.danger)
|
|
||||||
async def no_button(self, button:ui.Button, interaction:discord.Interaction):
|
|
||||||
message = await self.passed_info['interaction'].edit_original_response(content=f"Command cancelled.", view=None)
|
|
||||||
await message.delete(delay=3)
|
|
||||||
|
|
||||||
|
|
||||||
get_group = app_commands.Group(name='get', description="Retrieves information from the Pterodactyl API.")
|
|
||||||
|
|
||||||
@get_group.command(name='url', description="Retrieves the URL for the specified endpoint.")
|
|
||||||
@app_commands.guild_only()
|
|
||||||
async def retrieve_url(self, interaction: discord.Interaction, endpoint: str = None):
|
|
||||||
"""Retrieves the URL for the specified endpoint."""
|
|
||||||
try:
|
try:
|
||||||
if endpoint:
|
if endpoint:
|
||||||
url = await self.get_url(interaction.guild, endpoint)
|
url = await self.get_url(interaction.guild, endpoint)
|
||||||
|
@ -308,14 +187,12 @@ class Pterodactyl(commands.Cog):
|
||||||
configure = app_commands.Group(name="config", description="Configures the Pterodactyl cog.")
|
configure = app_commands.Group(name="config", description="Configures the Pterodactyl cog.")
|
||||||
|
|
||||||
@configure.command()
|
@configure.command()
|
||||||
@app_commands.guild_only()
|
|
||||||
async def api(self, interaction: discord.Interaction):
|
async def api(self, interaction: discord.Interaction):
|
||||||
"""Sets the information used to access the Pterdoactyl API."""
|
"""Sets the information used to access the Pterdoactyl API."""
|
||||||
await interaction.response.send_modal(self.APIConfigModal(self.config))
|
await interaction.response.send_modal(self.APIConfigModal(self.config))
|
||||||
|
|
||||||
@configure.command()
|
@configure.command()
|
||||||
@app_commands.guild_only()
|
async def startup(self, interaction: discord.Interaction):
|
||||||
async def update(self, interaction: discord.Interaction):
|
|
||||||
"""Sets the startup arguments for the update command."""
|
"""Sets the startup arguments for the update command."""
|
||||||
await interaction.response.send_modal(self.StartupConfigModal(self.config))
|
await interaction.response.send_modal(self.StartupConfigModal(self.config))
|
||||||
|
|
||||||
|
|
Reference in a new issue