fix: power commands now use aiohttp instead of requests

This commit is contained in:
Seaswimmer 2023-07-24 09:01:06 -04:00
parent 72e7d83bd7
commit 7451a2e625
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -135,9 +135,9 @@ class Pterodactyl(commands.Cog):
await interaction.response.defer(ephemeral=True, thinking=True) await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response() interaction_message = await interaction.original_response()
headers = await self.get_headers(interaction.guild) headers = await self.get_headers(interaction.guild)
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
requests_json = response.json() response_json = response.json()
current_status = requests_json['attributes']['current_state'] current_status = response_json['attributes']['current_state']
if current_status == "offline": if current_status == "offline":
passed_info = { passed_info = {
"headers": headers, "headers": headers,
@ -148,7 +148,7 @@ class Pterodactyl(commands.Cog):
"message": "Server starting...", "message": "Server starting...",
"completed_message": "Server started!" "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: else:
message = await interaction_message.edit(content="The server is already running!") message = await interaction_message.edit(content="The server is already running!")
await message.delete(delay=3) await message.delete(delay=3)
@ -160,9 +160,9 @@ class Pterodactyl(commands.Cog):
await interaction.response.defer(ephemeral=True, thinking=True) await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response() interaction_message = await interaction.original_response()
headers = await self.get_headers(interaction.guild) headers = await self.get_headers(interaction.guild)
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
requests_json = response.json() response_json = response.json()
current_status = requests_json['attributes']['current_state'] current_status = response_json['attributes']['current_state']
if current_status == "running": if current_status == "running":
passed_info = { passed_info = {
"headers": headers, "headers": headers,
@ -173,7 +173,7 @@ class Pterodactyl(commands.Cog):
"message": "Server restarting...", "message": "Server restarting...",
"completed_message": "Server restarted!" "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": elif current_status == "offline":
message = await interaction_message.edit(content="The server is offline!") message = await interaction_message.edit(content="The server is offline!")
await message.delete(delay=3) await message.delete(delay=3)
@ -188,9 +188,9 @@ class Pterodactyl(commands.Cog):
await interaction.response.defer(ephemeral=True, thinking=True) await interaction.response.defer(ephemeral=True, thinking=True)
interaction_message = await interaction.original_response() interaction_message = await interaction.original_response()
headers = await self.get_headers(interaction.guild) headers = await self.get_headers(interaction.guild)
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) async with requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
requests_json = response.json() response_json = response.json()
current_status = requests_json['attributes']['current_state'] current_status = response_json['attributes']['current_state']
if current_status == "running" or current_status == "starting": if current_status == "running" or current_status == "starting":
passed_info = { passed_info = {
"headers": headers, "headers": headers,
@ -201,7 +201,7 @@ class Pterodactyl(commands.Cog):
"message": "Server stopping...", "message": "Server stopping...",
"completed_message": "Server stopped!" "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": elif current_status == "offline":
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)
@ -262,25 +262,26 @@ class Pterodactyl(commands.Cog):
await message.delete(delay=3) await message.delete(delay=3)
class PowerButtons(ui.View): class PowerButtons(ui.View):
def __init__(self, timeout, passed_info): def __init__(self, timeout, passed_info, session: aiohttp.ClientSession):
super().__init__() super().__init__()
self.passed_info = passed_info self.passed_info = passed_info
self.session = session
self.config = Config.get_conf(None, cog_name='Pterodactyl', identifier=457581387213637448123567) self.config = Config.get_conf(None, cog_name='Pterodactyl', identifier=457581387213637448123567)
@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'] 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) message = await self.passed_info['interaction'].edit_original_response(content=self.passed_info['message'], view=None)
while True: while True:
async with aiohttp.ClientSession() as session: async with self.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=headers) as response: response_dict = await response.json()
response_dict = await response.json() if response_dict['attributes']['current_state'] == self.passed_info['target_signal']:
if response_dict['attributes']['current_state'] == self.passed_info['target_signal']: await message.edit(content=self.passed_info['completed_message'])
await message.edit(content=self.passed_info['completed_message']) break
break else:
else: await asyncio.sleep(1)
await asyncio.sleep(1) continue
continue continue
@ui.button(label="No", style=discord.ButtonStyle.danger) @ui.button(label="No", style=discord.ButtonStyle.danger)