misc: moved update command

This commit is contained in:
Seaswimmer 2023-07-16 13:35:17 -04:00
parent 88471438a5
commit 25e776c02c
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -50,6 +50,80 @@ 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
@app_commands.command(name="update", description="Updates the server.")
@app_commands.guild_only()
async def update(self, interaction: discord.Interaction):
"""Updates the server using the arguments provided in the server's configuration."""
await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response()
if await self.config.guild(interaction.guild).api_key() is None:
await interaction_message.edit(f"Something went wrong.\nError: `API Key not set.`", ephemeral=True)
raise LookupError("API Key not set.")
elif await self.config.guild(interaction.guild).startup_jar() is None:
await interaction_message.edit(f"Something went wrong.\nError: `Startup jar not set.`", ephemeral=True)
raise LookupError("Startup jar not set.")
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)
raise LookupError("Startup arguments not set.")
else:
api_key = await self.config.guild(interaction.guild).api_key()
startup_jar = await self.config.guild(interaction.guild).startup_jar()
startup_commands = await self.config.guild(interaction.guild).startup_arguments()
headers = await self.get_headers(interaction.guild)
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
response_dict = response.json()
list_var = requests.get(await self.get_url(interaction.guild, "startup"), headers=headers)
list_var_response_dict = list_var.json()
updater_startup_vars = [
{
"key": "FLAGS",
"value": startup_commands
},
{
"key": "SERVER_JARFILE",
"value": startup_jar
}
]
old_startup_vars = [
{
"key": "FLAGS",
"value": list_var_response_dict['data'][4]['attributes']['server_value']
},
{
"key": "SERVER_JARFILE",
"value": list_var_response_dict['data'][0]['attributes']['server_value']
}
]
if response_dict['attributes']['current_state'] == "offline":
for data in updater_startup_vars:
await self.put(await self.get_url(interaction.guild, "startup/variable"), headers, data)
requests.post(await self.get_url(interaction.guild, "power"), headers=headers, json={"signal": "start"})
await interaction_message.edit(content="Updater started...")
await asyncio.sleep(1)
while True:
async with aiohttp.ClientSession() as session:
async with session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
response_dict = await response.json()
if response_dict['attributes']['current_state'] == "offline":
await interaction_message.edit(content="Updater finished.")
break
else:
await asyncio.sleep(1)
continue
for data in old_startup_vars:
await self.put(await self.get_url(interaction.guild, "startup/variable"), headers, data)
await interaction_message.edit(content="Packwiz installer finished.\nUpdate process completed!")
elif response_dict['attributes']['current_state'] == "running" or response_dict['attributes']['current_state'] == "starting":
passed_info = {
"headers": headers,
"updater_startup_vars": updater_startup_vars,
"old_startup_vars": old_startup_vars,
"interaction": interaction,
"guild": interaction.guild,
}
await interaction_message.edit(content="The server is already running! Are you sure you'd like to stop the server for updates?", view=self.UpdateButtons(timeout=180, passed_info=passed_info))
power = app_commands.Group(name='power', description="Controls the server's power state.") power = app_commands.Group(name='power', description="Controls the server's power state.")
@power.command(name='start', description="Starts the server.") @power.command(name='start', description="Starts the server.")
@ -130,79 +204,6 @@ class Pterodactyl(commands.Cog):
message = await interaction_message.edit(content="The server is already offline!") message = await interaction_message.edit(content="The server is already offline!")
await message.delete(delay=3) await message.delete(delay=3)
@app_commands.command(name="update", description="Updates the server.")
@app_commands.guild_only()
async def update(self, interaction: discord.Interaction):
"""Updates the server using the arguments provided in the server's configuration."""
await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response()
if await self.config.guild(interaction.guild).api_key() is None:
await interaction_message.edit(f"Something went wrong.\nError: `API Key not set.`", ephemeral=True)
raise LookupError("API Key not set.")
elif await self.config.guild(interaction.guild).startup_jar() is None:
await interaction_message.edit(f"Something went wrong.\nError: `Startup jar not set.`", ephemeral=True)
raise LookupError("Startup jar not set.")
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)
raise LookupError("Startup arguments not set.")
else:
api_key = await self.config.guild(interaction.guild).api_key()
startup_jar = await self.config.guild(interaction.guild).startup_jar()
startup_commands = await self.config.guild(interaction.guild).startup_arguments()
headers = await self.get_headers(interaction.guild)
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
response_dict = response.json()
list_var = requests.get(await self.get_url(interaction.guild, "startup"), headers=headers)
list_var_response_dict = list_var.json()
updater_startup_vars = [
{
"key": "FLAGS",
"value": startup_commands
},
{
"key": "SERVER_JARFILE",
"value": startup_jar
}
]
old_startup_vars = [
{
"key": "FLAGS",
"value": list_var_response_dict['data'][4]['attributes']['server_value']
},
{
"key": "SERVER_JARFILE",
"value": list_var_response_dict['data'][0]['attributes']['server_value']
}
]
if response_dict['attributes']['current_state'] == "offline":
for data in updater_startup_vars:
await self.put(await self.get_url(interaction.guild, "startup/variable"), headers, data)
requests.post(await self.get_url(interaction.guild, "power"), headers=headers, json={"signal": "start"})
await interaction_message.edit(content="Updater started...")
await asyncio.sleep(1)
while True:
async with aiohttp.ClientSession() as session:
async with session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
response_dict = await response.json()
if response_dict['attributes']['current_state'] == "offline":
await interaction_message.edit(content="Updater finished.")
break
else:
await asyncio.sleep(1)
continue
for data in old_startup_vars:
await self.put(await self.get_url(interaction.guild, "startup/variable"), headers, data)
await interaction_message.edit(content="Packwiz installer finished.\nUpdate process completed!")
elif response_dict['attributes']['current_state'] == "running" or response_dict['attributes']['current_state'] == "starting":
passed_info = {
"headers": headers,
"updater_startup_vars": updater_startup_vars,
"old_startup_vars": old_startup_vars,
"interaction": interaction,
"guild": interaction.guild,
}
await interaction_message.edit(content="The server is already running! Are you sure you'd like to stop the server for updates?", view=self.UpdateButtons(timeout=180, passed_info=passed_info))
class UpdateButtons(ui.View): class UpdateButtons(ui.View):
def __init__(self, timeout, passed_info): def __init__(self, timeout, passed_info):
super().__init__() super().__init__()