feat: power commands now check if another power command is in progress and error if they are

This commit is contained in:
Seaswimmer 2023-07-24 09:02:15 -04:00
parent 7451a2e625
commit 20a6d89f3d
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -57,6 +57,9 @@ class Pterodactyl(commands.Cog):
@app_commands.guild_only()
async def update(self, interaction: discord.Interaction):
"""Updates the server using the arguments provided in the server's configuration."""
if await self.config.guild(self.passed_info['guild']).power_action_in_progress() is True:
await interaction.response.send_message(ephemeral=True, content="Power action already in progress!\nTry again later.")
return
session = self.session
await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response()
@ -99,6 +102,7 @@ class Pterodactyl(commands.Cog):
}
]
if response_dict['attributes']['current_state'] == "offline":
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(True)
for data in updater_startup_vars:
await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, json=data)
await session.post(await self.get_url(interaction.guild, "power"), headers=headers, json={"signal": "start"})
@ -116,6 +120,7 @@ class Pterodactyl(commands.Cog):
for data in old_startup_vars:
await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, json=data)
await interaction_message.edit(content="Updater finished.\nUpdate process completed!")
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(False)
elif response_dict['attributes']['current_state'] == "running" or response_dict['attributes']['current_state'] == "starting":
passed_info = {
"headers": headers,
@ -132,6 +137,9 @@ class Pterodactyl(commands.Cog):
@app_commands.guild_only()
async def start(self, interaction: discord.Interaction):
"""Starts the server."""
if await self.config.guild(self.passed_info['guild']).power_action_in_progress() is True:
await interaction.response.send_message(ephemeral=True, content="Power action already in progress!\nTry again later.")
return
await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response()
headers = await self.get_headers(interaction.guild)
@ -157,6 +165,9 @@ class Pterodactyl(commands.Cog):
@app_commands.guild_only()
async def restart(self, interaction: discord.Interaction):
"""Restarts the server."""
if await self.config.guild(self.passed_info['guild']).power_action_in_progress() is True:
await interaction.response.send_message(ephemeral=True, content="Power action already in progress!\nTry again later.")
return
await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response()
headers = await self.get_headers(interaction.guild)
@ -185,6 +196,9 @@ class Pterodactyl(commands.Cog):
@app_commands.guild_only()
async def stop(self, interaction: discord.Interaction):
"""Stops the server."""
if await self.config.guild(self.passed_info['guild']).power_action_in_progress() is True:
await interaction.response.send_message(ephemeral=True, content="Power action already in progress!\nTry again later.")
return
await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response()
headers = await self.get_headers(interaction.guild)
@ -215,6 +229,7 @@ class Pterodactyl(commands.Cog):
@ui.button(label="Yes", style=discord.ButtonStyle.success)
async def yes_button(self, button:ui.Button, interaction:discord.Interaction):
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(True)
session = self.session
await session.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=self.passed_info['headers'], json={"signal": "stop"})
await self.passed_info['interaction'].edit_original_response(content="Server stopping...", view=None)
@ -255,6 +270,7 @@ class Pterodactyl(commands.Cog):
else:
await asyncio.sleep(0.5)
continue
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(False)
@ui.button(label="No", style=discord.ButtonStyle.danger)
async def no_button(self, button:ui.Button, interaction:discord.Interaction):
@ -270,6 +286,7 @@ class Pterodactyl(commands.Cog):
@ui.button(label="Yes", style=discord.ButtonStyle.success)
async def yes_button(self, button:ui.Button, interaction:discord.Interaction):
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(True)
headers = self.passed_info['headers']
await self.session.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)
@ -282,7 +299,7 @@ class Pterodactyl(commands.Cog):
else:
await asyncio.sleep(1)
continue
continue
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(False)
@ui.button(label="No", style=discord.ButtonStyle.danger)
async def no_button(self, button:ui.Button, interaction:discord.Interaction):