From f10178e948f001b57abcced5733375dcb8b779a9 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 12:39:44 -0400 Subject: [PATCH 01/26] feat: update now uses aiohttp --- pterodactyl/ptero.py | 126 ++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 61 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index def4d8d..3c72c90 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -20,16 +20,23 @@ class Pterodactyl(commands.Cog): 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: + async def get_session(self, guild, endpoint = None): + if await self.config.guild(guild).server_id() is None: + raise LookupError("Server ID not set.") + elif await self.config.guild(guild).base_url() is None: + raise LookupError("Base URL not set.") + elif 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 + url = f"https://{await self.config.guild(guild).base_url}/api/client/servers/{await self.config.guild(guild).server_id}" + if endpoint: + url += '/' + endpoint + async with aiohttp.ClientSession(url=url, headers=headers) as session: + return session 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.""" @@ -54,12 +61,10 @@ 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.""" + session = await self.get_session(interaction.guild) 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: + if 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: @@ -68,11 +73,10 @@ class Pterodactyl(commands.Cog): else: 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() + async with session.get(await self.get_url(interaction.guild, "resources")) as response: + response_dict = response.json() + async with session.get(await self.get_url(interaction.guild, "startup")) as response: + list_var_response_dict = response.json() updater_startup_vars = [ { "key": "FLAGS", @@ -95,32 +99,31 @@ class Pterodactyl(commands.Cog): ] 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 session.put(await self.get_url(interaction.guild, "startup/variable"), data) + await session.post(await self.get_url(interaction.guild, "power"), 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 + async with session.get(await self.get_url(interaction.guild, "resources")) 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 session.put(await self.get_url(interaction.guild, "startup/variable"), data) + session.close() await interaction_message.edit(content="Updater 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)) + 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, session=session)) power = app_commands.Group(name='power', description="Controls the server's power state.") @@ -203,59 +206,60 @@ class Pterodactyl(commands.Cog): await message.delete(delay=3) class UpdateButtons(ui.View): - def __init__(self, timeout, passed_info): + def __init__(self, timeout, passed_info, session: aiohttp.ClientSession): super().__init__() self.passed_info = passed_info + self.session = session 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": "stop"}) + session = self.session + session.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), json={"signal": "stop"}) await self.passed_info['interaction'].edit_original_response(content="Server stopping...", 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'] == "offline": - await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!") - break - else: - await asyncio.sleep(2) - continue + async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources")) as response: + response_dict = await response.json() + if response_dict['attributes']['current_state'] == "offline": + await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!") + break + else: + await asyncio.sleep(2) + continue 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) - requests.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=headers, json={"signal": "start"}) + async with session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), data=data) as response: + pass + await session.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), json={"signal": "start"}) await self.passed_info['interaction'].edit_original_response(content="Updater started...") await asyncio.sleep(2) 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'] == "offline": - await self.passed_info['interaction'].edit_original_response(content="Updater finished!") - break - else: - await asyncio.sleep(1) - continue + async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources")) as response: + response_dict = await response.json() + if response_dict['attributes']['current_state'] == "offline": + await self.passed_info['interaction'].edit_original_response(content="Updater finished!") + break + else: + await asyncio.sleep(1) + continue 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 session.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), data) await asyncio.sleep(2) - requests.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=headers, json={"signal": "start"}) + await session.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), json={"signal": "start"}) await self.passed_info['interaction'].edit_original_response(content="Server starting...") 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'] == "running": - await self.passed_info['interaction'].edit_original_response(content="Server started!\nUpdate process completed!") - break - else: - await asyncio.sleep(1) - continue + async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources")) as response: + response_dict = await response.json() + if response_dict['attributes']['current_state'] == "running": + await self.passed_info['interaction'].edit_original_response(content="Server started!\nUpdate process completed!") + break + else: + await asyncio.sleep(1) + continue + session.close() @ui.button(label="No", style=discord.ButtonStyle.danger) async def no_button(self, button:ui.Button, interaction:discord.Interaction): + self.session.close() message = await self.passed_info['interaction'].edit_original_response(content=f"Command cancelled.", view=None) await message.delete(delay=3) From fed6790122b1e134882db6bc8033e6e3a319b109 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 12:49:58 -0400 Subject: [PATCH 02/26] fix: fixed power commands failing to load --- pterodactyl/ptero.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 3c72c90..ee03d11 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -128,7 +128,7 @@ class Pterodactyl(commands.Cog): power = app_commands.Group(name='power', description="Controls the server's power state.") @power.command(name='start', description="Starts the server.") - @power.guild_only() + @app_commands.guild_only() async def start(self, interaction: discord.Interaction): """Starts the server.""" await interaction.response.defer(ephemeral=True, thinking=True) @@ -153,7 +153,7 @@ class Pterodactyl(commands.Cog): await message.delete(delay=3) @power.command(name='restart', description="Restarts the server.") - @power.guild_only() + @app_commands.guild_only() async def restart(self, interaction: discord.Interaction): """Restarts the server.""" await interaction.response.defer(ephemeral=True, thinking=True) @@ -181,7 +181,7 @@ class Pterodactyl(commands.Cog): await message.delete(delay=3) @power.command(name='stop', description="Stops the server.") - @power.guild_only() + @app_commands.guild_only() async def stop(self, interaction: discord.Interaction): """Stops the server.""" await interaction.response.defer(ephemeral=True, thinking=True) From 45b6c94f061df7b579969debfcf6de96d83e139f Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 12:50:44 -0400 Subject: [PATCH 03/26] fix: same thing as last commit, just with get_url --- pterodactyl/ptero.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index ee03d11..9aeabe3 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -294,7 +294,7 @@ class Pterodactyl(commands.Cog): 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.") - @get_group.guild_only() + @app_commands.guild_only() async def retrieve_url(self, interaction: discord.Interaction, endpoint: str = None): """Retrieves the URL for the specified endpoint.""" try: From a2488a53bd8cad67cbe7a4ffe6a51d71ccf5abde Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 12:51:10 -0400 Subject: [PATCH 04/26] fix: and again! (configure commands this time) --- pterodactyl/ptero.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 9aeabe3..9d6acbd 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -310,13 +310,13 @@ class Pterodactyl(commands.Cog): configure = app_commands.Group(name="config", description="Configures the Pterodactyl cog.") @configure.command(name="api", description="Sets the information used to access the Pterodactyl API.") - @configure.guild_only() + @app_commands.guild_only() async def configure_api(self, interaction: discord.Interaction): """Sets the information used to access the Pterdoactyl API.""" await interaction.response.send_modal(self.APIConfigModal(self.config)) @configure.command(name="update", description="Sets the startup arguments for the update command.") - @configure.guild_only() + @app_commands.guild_only() async def configure_update(self, interaction: discord.Interaction): """Sets the startup arguments for the update command.""" await interaction.response.send_modal(self.StartupConfigModal(self.config)) From 0f73bbaabc0e24896ee26765fe598c37c56f30e0 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 12:52:59 -0400 Subject: [PATCH 05/26] fix: fixed get_session --- pterodactyl/ptero.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 9d6acbd..c0d686f 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -32,7 +32,7 @@ class Pterodactyl(commands.Cog): "Content-Type": "application/json", "Accept": "application/json" } - url = f"https://{await self.config.guild(guild).base_url}/api/client/servers/{await self.config.guild(guild).server_id}" + url = f"https://{await self.config.guild(guild).base_url()}/api/client/servers/{await self.config.guild(guild).server_id()}" if endpoint: url += '/' + endpoint async with aiohttp.ClientSession(url=url, headers=headers) as session: From 7727f789f2dc45b45e46596839403e96a4ab27d2 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 12:55:27 -0400 Subject: [PATCH 06/26] fix: fixed get_session trying to assign a kwarg that doesn't exist --- pterodactyl/ptero.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index c0d686f..540adaf 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -20,7 +20,7 @@ class Pterodactyl(commands.Cog): startup_arguments=None ) - async def get_session(self, guild, endpoint = None): + async def get_session(self, guild): if await self.config.guild(guild).server_id() is None: raise LookupError("Server ID not set.") elif await self.config.guild(guild).base_url() is None: @@ -32,10 +32,7 @@ class Pterodactyl(commands.Cog): "Content-Type": "application/json", "Accept": "application/json" } - url = f"https://{await self.config.guild(guild).base_url()}/api/client/servers/{await self.config.guild(guild).server_id()}" - if endpoint: - url += '/' + endpoint - async with aiohttp.ClientSession(url=url, headers=headers) as session: + async with aiohttp.ClientSession(headers=headers) as session: return session async def get_url(self, guild, endpoint = None): From 85fa243732e61cfba8179880a015ab9c99d656bd Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:04:02 -0400 Subject: [PATCH 07/26] fix: hopefully made update work properly --- pterodactyl/ptero.py | 61 ++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 540adaf..201378e 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -4,7 +4,8 @@ import discord import requests from discord import ui from discord.ext import commands -from redbot.core import commands, app_commands, Config +from redbot.core import Config, app_commands, commands + class Pterodactyl(commands.Cog): """Pterodactyl allows you to manage your Pterodactyl Panel from Discord.""" @@ -19,21 +20,24 @@ class Pterodactyl(commands.Cog): startup_jar=None, startup_arguments=None ) + self.session: aiohttp.ClientSession = None - async def get_session(self, guild): - if await self.config.guild(guild).server_id() is None: - raise LookupError("Server ID not set.") - elif await self.config.guild(guild).base_url() is None: - raise LookupError("Base URL not set.") - elif await self.config.guild(guild).api_key() is None: + async def cog_load(self): + self.session = aiohttp.ClientSession() + + async def cog_unload(self): + await self.session.close() + + 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" } - async with aiohttp.ClientSession(headers=headers) as session: - return session + return headers 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.""" @@ -58,7 +62,7 @@ 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.""" - session = await self.get_session(interaction.guild) + session = self.session await interaction.response.defer(ephemeral=True, thinking=True) interaction_message = await interaction.original_response() if await self.config.guild(interaction.guild).startup_jar() is None: @@ -70,9 +74,14 @@ class Pterodactyl(commands.Cog): else: startup_jar = await self.config.guild(interaction.guild).startup_jar() startup_commands = await self.config.guild(interaction.guild).startup_arguments() - async with session.get(await self.get_url(interaction.guild, "resources")) as response: + try: + headers = await self.get_headers(interaction.guild) + except LookupError as e: + await interaction_message.edit(f"Something went wrong.\nError: `{e}`", ephemeral=True) + return + async with session.get(await self.get_url(interaction.guild, "resources", headers=headers)) as response: response_dict = response.json() - async with session.get(await self.get_url(interaction.guild, "startup")) as response: + async with session.get(await self.get_url(interaction.guild, "startup", headers=headers)) as response: list_var_response_dict = response.json() updater_startup_vars = [ { @@ -96,12 +105,12 @@ class Pterodactyl(commands.Cog): ] if response_dict['attributes']['current_state'] == "offline": for data in updater_startup_vars: - await session.put(await self.get_url(interaction.guild, "startup/variable"), data) - await session.post(await self.get_url(interaction.guild, "power"), json={"signal": "start"}) + await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, data=data) + await session.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 session.get(await self.get_url(interaction.guild, "resources")) as response: + 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.") @@ -110,11 +119,11 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(1) continue for data in old_startup_vars: - await session.put(await self.get_url(interaction.guild, "startup/variable"), data) - session.close() + await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, data=data) await interaction_message.edit(content="Updater 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, @@ -212,10 +221,10 @@ class Pterodactyl(commands.Cog): @ui.button(label="Yes", style=discord.ButtonStyle.success) async def yes_button(self, button:ui.Button, interaction:discord.Interaction): session = self.session - session.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), json={"signal": "stop"}) + 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) while True: - async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources")) 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() if response_dict['attributes']['current_state'] == "offline": await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!") @@ -224,13 +233,13 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(2) continue for data in self.passed_info['updater_startup_vars']: - async with session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), data=data) as response: + async with session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data) as response: pass - await session.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), json={"signal": "start"}) + await session.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 asyncio.sleep(2) while True: - async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources")) 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() if response_dict['attributes']['current_state'] == "offline": await self.passed_info['interaction'].edit_original_response(content="Updater finished!") @@ -239,12 +248,12 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(1) continue for data in self.passed_info['old_startup_vars']: - await session.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), data) + await session.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data) await asyncio.sleep(2) - await session.post(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), json={"signal": "start"}) + await session.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...") while True: - async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources")) 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() if response_dict['attributes']['current_state'] == "running": await self.passed_info['interaction'].edit_original_response(content="Server started!\nUpdate process completed!") @@ -252,11 +261,9 @@ class Pterodactyl(commands.Cog): else: await asyncio.sleep(1) continue - session.close() @ui.button(label="No", style=discord.ButtonStyle.danger) async def no_button(self, button:ui.Button, interaction:discord.Interaction): - self.session.close() message = await self.passed_info['interaction'].edit_original_response(content=f"Command cancelled.", view=None) await message.delete(delay=3) From 5398fb77a8ad5b262394dac3890a82b31e407782 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:05:03 -0400 Subject: [PATCH 08/26] fix: fixed update command --- pterodactyl/ptero.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 201378e..e78185c 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -79,9 +79,9 @@ class Pterodactyl(commands.Cog): except LookupError as e: await interaction_message.edit(f"Something went wrong.\nError: `{e}`", ephemeral=True) return - async with session.get(await self.get_url(interaction.guild, "resources", headers=headers)) as response: + async with session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: response_dict = response.json() - async with session.get(await self.get_url(interaction.guild, "startup", headers=headers)) as response: + async with session.get(await self.get_url(interaction.guild, "startup"), headers=headers) as response: list_var_response_dict = response.json() updater_startup_vars = [ { From 601a9642c70cdd41085c9fef3b755ea211163a6b Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:08:15 -0400 Subject: [PATCH 09/26] fix: fixed update command again --- pterodactyl/ptero.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index e78185c..5d5b639 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -80,9 +80,9 @@ class Pterodactyl(commands.Cog): await interaction_message.edit(f"Something went wrong.\nError: `{e}`", ephemeral=True) return async with session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: - response_dict = response.json() + response_dict = await response.json() async with session.get(await self.get_url(interaction.guild, "startup"), headers=headers) as response: - list_var_response_dict = response.json() + list_var_response_dict = await response.json() updater_startup_vars = [ { "key": "FLAGS", From 6079ccd3711d41ce072cdcfa1bc99855562d77f6 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:11:26 -0400 Subject: [PATCH 10/26] fix: fixed a missing await --- pterodactyl/ptero.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 5d5b639..7fe32cd 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -221,7 +221,7 @@ class Pterodactyl(commands.Cog): @ui.button(label="Yes", style=discord.ButtonStyle.success) async def yes_button(self, button:ui.Button, interaction:discord.Interaction): session = self.session - session.post(await Pterodactyl.get_url(self, self.passed_info['guild'], "power"), headers=self.passed_info['headers'], json={"signal": "stop"}) + 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) while True: async with session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=self.passed_info['headers']) as response: From fa48b3a924db854d80af91bb431f42028105f4f1 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:13:35 -0400 Subject: [PATCH 11/26] fix: changed something in the updatebuttons class --- pterodactyl/ptero.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 7fe32cd..f73221a 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -233,8 +233,7 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(2) continue for data in self.passed_info['updater_startup_vars']: - async with session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data) as response: - pass + await session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data) await session.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 asyncio.sleep(2) From 1bf60cef819e66f7cd7552c4424ef4e499cdac29 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:29:07 -0400 Subject: [PATCH 12/26] fix: testing a fix for update --- pterodactyl/ptero.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index f73221a..f4f09c3 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -105,7 +105,7 @@ class Pterodactyl(commands.Cog): ] if response_dict['attributes']['current_state'] == "offline": for data in updater_startup_vars: - await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, data=data) + 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"}) await interaction_message.edit(content="Updater started...") await asyncio.sleep(1) @@ -119,7 +119,7 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(1) continue for data in old_startup_vars: - await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, data=data) + 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!") elif response_dict['attributes']['current_state'] == "running" or response_dict['attributes']['current_state'] == "starting": passed_info = { From ef662d81b1656e3abe0aa3ee2dd6fb0990a5f7ac Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:32:49 -0400 Subject: [PATCH 13/26] fix: applied previous fix to UpdateButtons --- pterodactyl/ptero.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index f4f09c3..57dc7ea 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -233,7 +233,7 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(2) continue for data in self.passed_info['updater_startup_vars']: - await session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data) + await session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) await session.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 asyncio.sleep(2) @@ -247,7 +247,7 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(1) continue for data in self.passed_info['old_startup_vars']: - await session.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], data=data) + await session.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) await asyncio.sleep(2) await session.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...") From 35e80fdbb3c8a7c4179a12ebb0c67d227031a50a Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 14:34:58 -0400 Subject: [PATCH 14/26] misc: changed sleep duration on UpdateButtons to 1 second --- pterodactyl/ptero.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 57dc7ea..8533033 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -230,7 +230,7 @@ class Pterodactyl(commands.Cog): await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!") break else: - await asyncio.sleep(2) + await asyncio.sleep(1) continue for data in self.passed_info['updater_startup_vars']: await session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) From fb99f5ea3353b00cc4476cdb2b1f9cd485f0aa2f Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 23 Jul 2023 23:09:34 -0400 Subject: [PATCH 15/26] misc: reduced sleep timer for server status checking --- pterodactyl/ptero.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 8533033..b0defb2 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -116,7 +116,7 @@ class Pterodactyl(commands.Cog): await interaction_message.edit(content="Updater finished.") break else: - await asyncio.sleep(1) + await asyncio.sleep(0.5) continue for data in old_startup_vars: await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, json=data) @@ -230,7 +230,7 @@ class Pterodactyl(commands.Cog): await self.passed_info['interaction'].edit_original_response(content="\nServer stopped!") break else: - await asyncio.sleep(1) + await asyncio.sleep(0.5) continue for data in self.passed_info['updater_startup_vars']: await session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) @@ -258,7 +258,7 @@ class Pterodactyl(commands.Cog): await self.passed_info['interaction'].edit_original_response(content="Server started!\nUpdate process completed!") break else: - await asyncio.sleep(1) + await asyncio.sleep(0.5) continue @ui.button(label="No", style=discord.ButtonStyle.danger) From d5b4ac3dbe82e6c7f649ca3aac900e5de8d6d90c Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 08:45:53 -0400 Subject: [PATCH 16/26] fix: hopefully fixed the broken update command --- pterodactyl/ptero.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index b0defb2..8f4decc 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -233,7 +233,7 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(0.5) continue for data in self.passed_info['updater_startup_vars']: - await session.put(self, url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) + await session.put(url=await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) await session.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 asyncio.sleep(2) @@ -247,7 +247,7 @@ class Pterodactyl(commands.Cog): await asyncio.sleep(1) continue for data in self.passed_info['old_startup_vars']: - await session.put(self, await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) + await session.put(await Pterodactyl.get_url(self, self.passed_info['guild'], "startup/variable"), headers=self.passed_info['headers'], json=data) await asyncio.sleep(2) await session.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...") From ae4d7942585b50f7acf56d63eff1e66a247f5dae Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 08:59:01 -0400 Subject: [PATCH 17/26] feat: added additional configuration option - used internally --- pterodactyl/ptero.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 8f4decc..9437702 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -18,7 +18,8 @@ class Pterodactyl(commands.Cog): api_key=None, server_id=None, startup_jar=None, - startup_arguments=None + startup_arguments=None, + power_action_in_progress=False ) self.session: aiohttp.ClientSession = None From 72e7d83bd7567ec67f0b9d6b913a781563a27f4c Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 08:59:45 -0400 Subject: [PATCH 18/26] cleanup: removed obsolete method --- pterodactyl/ptero.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 9437702..cee959d 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -53,12 +53,6 @@ class Pterodactyl(commands.Cog): url += '/' + endpoint return url - async def put(self, url: str, headers: dict, data: dict): - """Sends an asyncio PUT request to the specified URL with the specified headers and data.""" - async with aiohttp.ClientSession() as session: - async with session.put(url, headers=headers, json=data) as response: - return response - @app_commands.command(name="update", description="Updates the server.") @app_commands.guild_only() async def update(self, interaction: discord.Interaction): From 7451a2e625058f7ab908131a4fd9b769c6c2c8a9 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:01:06 -0400 Subject: [PATCH 19/26] fix: power commands now use aiohttp instead of requests --- pterodactyl/ptero.py | 45 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index cee959d..a578d83 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -135,9 +135,9 @@ class Pterodactyl(commands.Cog): await interaction.response.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'] + async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: + response_json = response.json() + current_status = response_json['attributes']['current_state'] if current_status == "offline": passed_info = { "headers": headers, @@ -148,7 +148,7 @@ class Pterodactyl(commands.Cog): "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)) + await interaction_message.edit(content="Are you sure you'd like to start the server?", view=self.PowerButtons(timeout=180, passed_info=passed_info, session=self.session)) else: message = await interaction_message.edit(content="The server is already running!") await message.delete(delay=3) @@ -160,9 +160,9 @@ class Pterodactyl(commands.Cog): await interaction.response.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'] + async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: + response_json = response.json() + current_status = response_json['attributes']['current_state'] if current_status == "running": passed_info = { "headers": headers, @@ -173,7 +173,7 @@ class Pterodactyl(commands.Cog): "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)) + await interaction_message.edit(content="Are you sure you'd like to restart the server?", view=self.PowerButtons(timeout=180, passed_info=passed_info, session=self.session)) elif current_status == "offline": message = await interaction_message.edit(content="The server is offline!") await message.delete(delay=3) @@ -188,9 +188,9 @@ class Pterodactyl(commands.Cog): await interaction.response.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'] + async with requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: + response_json = response.json() + current_status = response_json['attributes']['current_state'] if current_status == "running" or current_status == "starting": passed_info = { "headers": headers, @@ -201,7 +201,7 @@ class Pterodactyl(commands.Cog): "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)) + await interaction_message.edit(content="Are you sure you'd like to stop the server?", view=self.PowerButtons(timeout=180, passed_info=passed_info, session=self.session)) elif current_status == "offline": message = await interaction_message.edit(content="The server is already offline!") await message.delete(delay=3) @@ -262,25 +262,26 @@ class Pterodactyl(commands.Cog): await message.delete(delay=3) class PowerButtons(ui.View): - def __init__(self, timeout, passed_info): + def __init__(self, timeout, passed_info, session: aiohttp.ClientSession): super().__init__() self.passed_info = passed_info + self.session = session 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']}) + 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) 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) + async with self.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 continue @ui.button(label="No", style=discord.ButtonStyle.danger) From 20a6d89f3da46ebd207d1e8dc56f4a6ad9aba441 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:02:15 -0400 Subject: [PATCH 20/26] feat: power commands now check if another power command is in progress and error if they are --- pterodactyl/ptero.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index a578d83..be7419f 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -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): From dcd3560fbca792d9a81144ff15da383f9e67d320 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:04:58 -0400 Subject: [PATCH 21/26] fix: fixed power and update commands' error checking --- pterodactyl/ptero.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index be7419f..428e497 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -57,7 +57,7 @@ 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: + if await self.config.guild(interaction.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 @@ -102,7 +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) + await self.config.guild(interaction.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"}) @@ -120,7 +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) + await self.config.guild(interaction.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, @@ -137,7 +137,7 @@ 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: + if await self.config.guild(interaction.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) @@ -165,7 +165,7 @@ 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: + if await self.config.guild(interaction.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) @@ -196,7 +196,7 @@ 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: + if await self.config.guild(interaction.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) From 3ed6cffe5d98180f8e8e2778483bd70a90760aa3 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:06:56 -0400 Subject: [PATCH 22/26] fix: awaited some things inside power commands --- pterodactyl/ptero.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 428e497..bb3e74c 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -144,7 +144,7 @@ class Pterodactyl(commands.Cog): interaction_message = await interaction.original_response() headers = await self.get_headers(interaction.guild) async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: - response_json = response.json() + response_json = await response.json() current_status = response_json['attributes']['current_state'] if current_status == "offline": passed_info = { @@ -172,7 +172,7 @@ class Pterodactyl(commands.Cog): interaction_message = await interaction.original_response() headers = await self.get_headers(interaction.guild) async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: - response_json = response.json() + response_json = await response.json() current_status = response_json['attributes']['current_state'] if current_status == "running": passed_info = { @@ -203,7 +203,7 @@ class Pterodactyl(commands.Cog): interaction_message = await interaction.original_response() headers = await self.get_headers(interaction.guild) async with requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: - response_json = response.json() + response_json = await response.json() current_status = response_json['attributes']['current_state'] if current_status == "running" or current_status == "starting": passed_info = { From db764b54e2cd97c15c2bc7a0e6bd1f4d28acf4a7 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:16:17 -0400 Subject: [PATCH 23/26] fix: fixed power_action_in_progress --- pterodactyl/ptero.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index bb3e74c..237a635 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -102,7 +102,7 @@ class Pterodactyl(commands.Cog): } ] if response_dict['attributes']['current_state'] == "offline": - await self.config.guild(interaction.guild).power_action_in_progress().set(True) + await self.config.guild(interaction.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"}) @@ -120,7 +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(interaction.guild).power_action_in_progress().set(False) + await self.config.guild(interaction.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, @@ -229,7 +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) + 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) @@ -270,7 +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) + 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): @@ -286,7 +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) + 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) @@ -299,7 +299,7 @@ class Pterodactyl(commands.Cog): else: await asyncio.sleep(1) continue - await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(False) + 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): From 6ec35134fd25018a1f7242dc2b6a1131d3860275 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:17:25 -0400 Subject: [PATCH 24/26] fix: added a 10s delay to the power commands --- pterodactyl/ptero.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 237a635..7910537 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -290,6 +290,7 @@ class Pterodactyl(commands.Cog): 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) + await asyncio.sleep(10) while True: async with self.session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=headers) as response: response_dict = await response.json() From 610e9902a9618bff85c05dfa30c2a39e911b7663 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:25:00 -0400 Subject: [PATCH 25/26] fix: stop was using requests still and erroring because of it --- pterodactyl/ptero.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 7910537..821c8a7 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -202,7 +202,7 @@ class Pterodactyl(commands.Cog): await interaction.response.defer(ephemeral=True, thinking=True) interaction_message = await interaction.original_response() headers = await self.get_headers(interaction.guild) - async with requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: + async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response: response_json = await response.json() current_status = response_json['attributes']['current_state'] if current_status == "running" or current_status == "starting": From ec5daee2de64f17316930d30d43c1b24d840534f Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 24 Jul 2023 09:25:09 -0400 Subject: [PATCH 26/26] cleanup: removed requests from imports --- pterodactyl/ptero.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pterodactyl/ptero.py b/pterodactyl/ptero.py index 821c8a7..ed0e383 100644 --- a/pterodactyl/ptero.py +++ b/pterodactyl/ptero.py @@ -1,7 +1,6 @@ import asyncio import aiohttp import discord -import requests from discord import ui from discord.ext import commands from redbot.core import Config, app_commands, commands