feat(pterodactyl): use red's api key storage system for api key storage and not a config key

This commit is contained in:
Seaswimmer 2024-03-01 01:17:31 -05:00
parent 7e03696e10
commit afc5868e9e
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063
2 changed files with 19 additions and 11 deletions

View file

@ -1,6 +1,6 @@
import asyncio import asyncio
import json import json
from typing import Optional from typing import Mapping, Optional
import discord import discord
import websockets import websockets
@ -76,6 +76,13 @@ class Pterodactyl(commands.Cog):
command = command.replace(".$U", username).replace(".$M", message).replace(".$C", str(color)) command = command.replace(".$U", username).replace(".$M", message).replace(".$C", str(color))
return command return command
@commands.Cog.listener()
async def on_red_api_tokens_update(self, service_name: str, api_tokens: Mapping[str,str]):
if service_name == "pterodactyl":
logger.info("Configuration value set: api_key\nRestarting task...")
self.task.cancel()
self.task = self.get_task()
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"]) @commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"])
async def pterodactyl(self, ctx: commands.Context) -> None: async def pterodactyl(self, ctx: commands.Context) -> None:
"""Pterodactyl allows you to manage your Pterodactyl Panel from Discord.""" """Pterodactyl allows you to manage your Pterodactyl Panel from Discord."""
@ -100,15 +107,6 @@ class Pterodactyl(commands.Cog):
self.task.cancel() self.task.cancel()
self.task = self.get_task() self.task = self.get_task()
@pterodactyl_config.command(name = "apikey")
async def pterodactyl_config_api_key(self, ctx: commands.Context, *, api_key: str) -> None:
"""Set the API key for your Pterodactyl Panel."""
await config.api_key.set(api_key)
await ctx.send(f"API key set to `{api_key[:5]}...{api_key[-4:]}`")
logger.info("Configuration value set: api_key = %s\nRestarting task...", api_key)
self.task.cancel()
self.task = self.get_task()
@pterodactyl_config.command(name = "serverid") @pterodactyl_config.command(name = "serverid")
async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None: async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None:
"""Set the server ID for your Pterodactyl Panel.""" """Set the server ID for your Pterodactyl Panel."""

View file

@ -114,9 +114,19 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
await chat.send(await config.shutdown_msg()) await chat.send(await config.shutdown_msg())
async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[dict]: async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[dict]:
pterodactyl_keys = await coginstance.bot.get_shared_api_tokens("pterodactyl")
api_key = pterodactyl_keys.get("api_key")
if api_key is None:
coginstance.task.cancel()
return logger.error("Pterodactyl API key not set. Please set it using `[p]set api`.")
base_url = await config.base_url() base_url = await config.base_url()
api_key = await config.api_key() if base_url is None:
coginstance.task.cancel()
return logger.error("Pterodactyl base URL not set. Please set it using `[p]ptero set url`.")
server_id = await config.server_id() server_id = await config.server_id()
if server_id is None:
coginstance.task.cancel()
return logger.error("Pterodactyl server ID not set. Please set it using `[p]ptero set serverid`.")
try: try:
client = PterodactylClient(base_url, api_key, debug=True).client client = PterodactylClient(base_url, api_key, debug=True).client