Compare commits
No commits in common. "20a6d89f3da46ebd207d1e8dc56f4a6ad9aba441" and "d5b4ac3dbe82e6c7f649ca3aac900e5de8d6d90c" have entirely different histories.
20a6d89f3d
...
d5b4ac3dbe
1 changed files with 30 additions and 43 deletions
|
@ -18,8 +18,7 @@ class Pterodactyl(commands.Cog):
|
||||||
api_key=None,
|
api_key=None,
|
||||||
server_id=None,
|
server_id=None,
|
||||||
startup_jar=None,
|
startup_jar=None,
|
||||||
startup_arguments=None,
|
startup_arguments=None
|
||||||
power_action_in_progress=False
|
|
||||||
)
|
)
|
||||||
self.session: aiohttp.ClientSession = None
|
self.session: aiohttp.ClientSession = None
|
||||||
|
|
||||||
|
@ -53,13 +52,16 @@ class Pterodactyl(commands.Cog):
|
||||||
url += '/' + endpoint
|
url += '/' + endpoint
|
||||||
return url
|
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.command(name="update", description="Updates the server.")
|
||||||
@app_commands.guild_only()
|
@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."""
|
||||||
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
|
session = self.session
|
||||||
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()
|
||||||
|
@ -102,7 +104,6 @@ class Pterodactyl(commands.Cog):
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
if response_dict['attributes']['current_state'] == "offline":
|
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:
|
for data in updater_startup_vars:
|
||||||
await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, json=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 session.post(await self.get_url(interaction.guild, "power"), headers=headers, json={"signal": "start"})
|
||||||
|
@ -120,7 +121,6 @@ class Pterodactyl(commands.Cog):
|
||||||
for data in old_startup_vars:
|
for data in old_startup_vars:
|
||||||
await session.put(await self.get_url(interaction.guild, "startup/variable"), headers=headers, json=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!")
|
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":
|
elif response_dict['attributes']['current_state'] == "running" or response_dict['attributes']['current_state'] == "starting":
|
||||||
passed_info = {
|
passed_info = {
|
||||||
"headers": headers,
|
"headers": headers,
|
||||||
|
@ -137,15 +137,12 @@ class Pterodactyl(commands.Cog):
|
||||||
@app_commands.guild_only()
|
@app_commands.guild_only()
|
||||||
async def start(self, interaction: discord.Interaction):
|
async def start(self, interaction: discord.Interaction):
|
||||||
"""Starts the server."""
|
"""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)
|
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)
|
||||||
async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
|
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
||||||
response_json = response.json()
|
requests_json = response.json()
|
||||||
current_status = response_json['attributes']['current_state']
|
current_status = requests_json['attributes']['current_state']
|
||||||
if current_status == "offline":
|
if current_status == "offline":
|
||||||
passed_info = {
|
passed_info = {
|
||||||
"headers": headers,
|
"headers": headers,
|
||||||
|
@ -156,7 +153,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, session=self.session))
|
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:
|
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)
|
||||||
|
@ -165,15 +162,12 @@ class Pterodactyl(commands.Cog):
|
||||||
@app_commands.guild_only()
|
@app_commands.guild_only()
|
||||||
async def restart(self, interaction: discord.Interaction):
|
async def restart(self, interaction: discord.Interaction):
|
||||||
"""Restarts the server."""
|
"""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)
|
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)
|
||||||
async with self.session.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
|
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
||||||
response_json = response.json()
|
requests_json = response.json()
|
||||||
current_status = response_json['attributes']['current_state']
|
current_status = requests_json['attributes']['current_state']
|
||||||
if current_status == "running":
|
if current_status == "running":
|
||||||
passed_info = {
|
passed_info = {
|
||||||
"headers": headers,
|
"headers": headers,
|
||||||
|
@ -184,7 +178,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, session=self.session))
|
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":
|
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)
|
||||||
|
@ -196,15 +190,12 @@ class Pterodactyl(commands.Cog):
|
||||||
@app_commands.guild_only()
|
@app_commands.guild_only()
|
||||||
async def stop(self, interaction: discord.Interaction):
|
async def stop(self, interaction: discord.Interaction):
|
||||||
"""Stops the server."""
|
"""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)
|
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)
|
||||||
async with requests.get(await self.get_url(interaction.guild, "resources"), headers=headers) as response:
|
response = requests.get(await self.get_url(interaction.guild, "resources"), headers=headers)
|
||||||
response_json = response.json()
|
requests_json = response.json()
|
||||||
current_status = response_json['attributes']['current_state']
|
current_status = requests_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,
|
||||||
|
@ -215,7 +206,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, session=self.session))
|
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":
|
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)
|
||||||
|
@ -229,7 +220,6 @@ 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):
|
||||||
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(True)
|
|
||||||
session = self.session
|
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 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)
|
await self.passed_info['interaction'].edit_original_response(content="Server stopping...", view=None)
|
||||||
|
@ -270,7 +260,6 @@ class Pterodactyl(commands.Cog):
|
||||||
else:
|
else:
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
continue
|
continue
|
||||||
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(False)
|
|
||||||
|
|
||||||
@ui.button(label="No", style=discord.ButtonStyle.danger)
|
@ui.button(label="No", style=discord.ButtonStyle.danger)
|
||||||
async def no_button(self, button:ui.Button, interaction:discord.Interaction):
|
async def no_button(self, button:ui.Button, interaction:discord.Interaction):
|
||||||
|
@ -278,20 +267,19 @@ 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, session: aiohttp.ClientSession):
|
def __init__(self, timeout, passed_info):
|
||||||
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):
|
||||||
await self.config.guild(self.passed_info['guild']).power_action_in_progress().set(True)
|
|
||||||
headers = self.passed_info['headers']
|
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']})
|
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)
|
message = await self.passed_info['interaction'].edit_original_response(content=self.passed_info['message'], view=None)
|
||||||
while True:
|
while True:
|
||||||
async with self.session.get(await Pterodactyl.get_url(self, self.passed_info['guild'], "resources"), headers=headers) as response:
|
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()
|
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'])
|
||||||
|
@ -299,7 +287,6 @@ class Pterodactyl(commands.Cog):
|
||||||
else:
|
else:
|
||||||
await asyncio.sleep(1)
|
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)
|
@ui.button(label="No", style=discord.ButtonStyle.danger)
|
||||||
async def no_button(self, button:ui.Button, interaction:discord.Interaction):
|
async def no_button(self, button:ui.Button, interaction:discord.Interaction):
|
||||||
|
|
Reference in a new issue