fix(pterodactyl): update pterodactyl for the new version of websockets & fix a few minor bugs
This commit is contained in:
parent
d51e3f17e9
commit
ff9e20be91
3 changed files with 66 additions and 50 deletions
|
@ -1,8 +1,8 @@
|
||||||
from red_commons import logging
|
from red_commons import logging
|
||||||
from red_commons.logging import getLogger
|
from red_commons.logging import getLogger
|
||||||
|
|
||||||
logger = getLogger('red.SeaCogs.Pterodactyl')
|
logger = getLogger("red.SeaCogs.Pterodactyl")
|
||||||
websocket_logger = getLogger('red.SeaCogs.Pterodactyl.websocket')
|
websocket_logger = getLogger("red.SeaCogs.Pterodactyl.Websocket")
|
||||||
if logger.level >= logging.VERBOSE:
|
if logger.level >= logging.VERBOSE:
|
||||||
websocket_logger.setLevel(logging.logging.INFO)
|
websocket_logger.setLevel(logging.logging.INFO)
|
||||||
elif logger.level < logging.VERBOSE:
|
elif logger.level < logging.VERBOSE:
|
||||||
|
|
|
@ -22,14 +22,14 @@ class Pterodactyl(commands.Cog):
|
||||||
|
|
||||||
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
|
__author__ = ["[cswimr](https://www.coastalcommits.com/cswimr)"]
|
||||||
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
|
__git__ = "https://www.coastalcommits.com/cswimr/SeaCogs"
|
||||||
__version__ = "2.0.4"
|
__version__ = "2.0.5"
|
||||||
__documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/"
|
__documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/"
|
||||||
|
|
||||||
def __init__(self, bot: Red):
|
def __init__(self, bot: Red):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.client: Optional[PterodactylClient] = None
|
self.client: Optional[PterodactylClient] = None
|
||||||
self.task: Optional[asyncio.Task] = None
|
self.task: Optional[asyncio.Task] = None
|
||||||
self.websocket: Optional[websockets.WebSocketClientProtocol] = None
|
self.websocket: Optional[websockets.ClientConnection] = None
|
||||||
self.retry_counter: int = 0
|
self.retry_counter: int = 0
|
||||||
register_config(config)
|
register_config(config)
|
||||||
self.task = self.get_task()
|
self.task = self.get_task()
|
||||||
|
@ -51,15 +51,18 @@ class Pterodactyl(commands.Cog):
|
||||||
api_key = pterodactyl_keys.get("api_key")
|
api_key = pterodactyl_keys.get("api_key")
|
||||||
if api_key is None:
|
if api_key is None:
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
raise ValueError("Pterodactyl API key not set. Please set it using `[p]set api`.")
|
logger.error("Pterodactyl API key not set. Please set it using `[p]set api`.")
|
||||||
|
return
|
||||||
base_url = await config.base_url()
|
base_url = await config.base_url()
|
||||||
if base_url is None:
|
if base_url is None:
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
raise ValueError("Pterodactyl base URL not set. Please set it using `[p]pterodactyl config url`.")
|
logger.error("Pterodactyl base URL not set. Please set it using `[p]pterodactyl config url`.")
|
||||||
|
return
|
||||||
server_id = await config.server_id()
|
server_id = await config.server_id()
|
||||||
if server_id is None:
|
if server_id is None:
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
raise ValueError("Pterodactyl server ID not set. Please set it using `[p]pterodactyl config serverid`.")
|
logger.error("Pterodactyl server ID not set. Please set it using `[p]pterodactyl config serverid`.")
|
||||||
|
return
|
||||||
|
|
||||||
self.client = PterodactylClient(base_url, api_key).client
|
self.client = PterodactylClient(base_url, api_key).client
|
||||||
|
|
||||||
|
@ -67,7 +70,6 @@ class Pterodactyl(commands.Cog):
|
||||||
self.update_topic.cancel()
|
self.update_topic.cancel()
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
self.retry_counter = 0
|
self.retry_counter = 0
|
||||||
await self.client._session.close() # pylint: disable=protected-access # noqa: SLF001
|
|
||||||
|
|
||||||
def get_task(self) -> asyncio.Task:
|
def get_task(self) -> asyncio.Task:
|
||||||
from pterodactyl.websocket import establish_websocket_connection
|
from pterodactyl.websocket import establish_websocket_connection
|
||||||
|
@ -76,7 +78,7 @@ class Pterodactyl(commands.Cog):
|
||||||
task.add_done_callback(self.error_callback)
|
task.add_done_callback(self.error_callback)
|
||||||
return task
|
return task
|
||||||
|
|
||||||
def error_callback(self, fut) -> None: # NOTE - Thanks flame442 and zephyrkul for helping me figure this out
|
def error_callback(self, fut) -> None: # NOTE Thanks flame442 and zephyrkul for helping me figure this out
|
||||||
try:
|
try:
|
||||||
fut.result()
|
fut.result()
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
|
|
@ -6,10 +6,10 @@ from typing import Optional, Tuple, Union
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
import websockets
|
|
||||||
from pydactyl import PterodactylClient
|
from pydactyl import PterodactylClient
|
||||||
from redbot.core.data_manager import bundled_data_path
|
from redbot.core.data_manager import bundled_data_path
|
||||||
from redbot.core.utils.chat_formatting import bold, pagify
|
from redbot.core.utils.chat_formatting import bold, pagify
|
||||||
|
from websockets.asyncio.client import connect
|
||||||
|
|
||||||
from pterodactyl.config import config
|
from pterodactyl.config import config
|
||||||
from pterodactyl.logger import logger, websocket_logger
|
from pterodactyl.logger import logger, websocket_logger
|
||||||
|
@ -19,46 +19,46 @@ from pterodactyl.pterodactyl import Pterodactyl
|
||||||
async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
|
async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
|
||||||
await coginstance.bot.wait_until_red_ready()
|
await coginstance.bot.wait_until_red_ready()
|
||||||
base_url = await config.base_url()
|
base_url = await config.base_url()
|
||||||
base_url = base_url[:-1] if base_url.endswith('/') else base_url
|
base_url = base_url[:-1] if base_url.endswith("/") else base_url
|
||||||
|
|
||||||
logger.info("Establishing WebSocket connection")
|
logger.info("Establishing WebSocket connection")
|
||||||
|
|
||||||
websocket_credentials = await retrieve_websocket_credentials(coginstance)
|
websocket_credentials = await retrieve_websocket_credentials(coginstance)
|
||||||
|
|
||||||
async with websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60, logger=websocket_logger) as websocket:
|
async with connect(websocket_credentials["data"]["socket"], origin=base_url, ping_timeout=60, logger=websocket_logger) as websocket:
|
||||||
logger.info("WebSocket connection established")
|
logger.info("WebSocket connection established")
|
||||||
|
|
||||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials["data"]["token"]]})
|
||||||
await websocket.send(auth_message)
|
await websocket.send(auth_message)
|
||||||
logger.info("Authentication message sent")
|
logger.info("Authentication message sent")
|
||||||
|
|
||||||
coginstance.websocket = websocket
|
coginstance.websocket = websocket
|
||||||
|
|
||||||
while True: # pylint: disable=too-many-nested-blocks
|
while True: # pylint: disable=too-many-nested-blocks
|
||||||
message = json.loads(await websocket.recv())
|
message = json.loads(await websocket.recv())
|
||||||
if message['event'] in ('token expiring', 'token expired'):
|
if message["event"] in ("token expiring", "token expired"):
|
||||||
logger.info("Received token expiring/expired event. Refreshing token.")
|
logger.info("Received token expiring/expired event. Refreshing token.")
|
||||||
websocket_credentials = await retrieve_websocket_credentials(coginstance)
|
websocket_credentials = await retrieve_websocket_credentials(coginstance)
|
||||||
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials['data']['token']]})
|
auth_message = json.dumps({"event": "auth", "args": [websocket_credentials["data"]["token"]]})
|
||||||
await websocket.send(auth_message)
|
await websocket.send(auth_message)
|
||||||
logger.info("Authentication message sent")
|
logger.info("Authentication message sent")
|
||||||
|
|
||||||
if message['event'] == 'auth success':
|
if message["event"] == "auth success":
|
||||||
logger.info("WebSocket authentication successful")
|
logger.info("WebSocket authentication successful")
|
||||||
|
|
||||||
if message['event'] == 'console output' and await config.console_channel() is not None:
|
if message["event"] == "console output" and await config.console_channel() is not None:
|
||||||
regex_blacklist: dict = await config.regex_blacklist()
|
regex_blacklist: dict = await config.regex_blacklist()
|
||||||
matches = [re.search(regex, message['args'][0]) for regex in regex_blacklist.values()]
|
matches = [re.search(regex, message["args"][0]) for regex in regex_blacklist.values()]
|
||||||
|
|
||||||
if await config.current_status() in ('running', '') and not any(matches):
|
if await config.current_status() in ("running", "") and not any(matches):
|
||||||
content = remove_ansi_escape_codes(message['args'][0])
|
content = remove_ansi_escape_codes(message["args"][0])
|
||||||
if await config.mask_ip() is True:
|
if await config.mask_ip() is True:
|
||||||
content = mask_ip(content)
|
content = mask_ip(content)
|
||||||
|
|
||||||
console_channel = coginstance.bot.get_channel(await config.console_channel())
|
console_channel = coginstance.bot.get_channel(await config.console_channel())
|
||||||
chat_channel = coginstance.bot.get_channel(await config.chat_channel())
|
chat_channel = coginstance.bot.get_channel(await config.chat_channel())
|
||||||
if console_channel is not None:
|
if console_channel is not None:
|
||||||
if content.startswith('['):
|
if content.startswith("["):
|
||||||
pagified_content = pagify(content, delims=[" ", "\n"])
|
pagified_content = pagify(content, delims=[" ", "\n"])
|
||||||
for page in pagified_content:
|
for page in pagified_content:
|
||||||
await console_channel.send(content=page, allowed_mentions=discord.AllowedMentions.none())
|
await console_channel.send(content=page, allowed_mentions=discord.AllowedMentions.none())
|
||||||
|
@ -66,23 +66,23 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
|
||||||
server_message = await check_if_server_message(content)
|
server_message = await check_if_server_message(content)
|
||||||
if server_message:
|
if server_message:
|
||||||
if chat_channel is not None:
|
if chat_channel is not None:
|
||||||
await chat_channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + '...', allowed_mentions=discord.AllowedMentions.none())
|
await chat_channel.send(server_message if len(server_message) < 2000 else server_message[:1997] + "...", allowed_mentions=discord.AllowedMentions.none())
|
||||||
|
|
||||||
chat_message = await check_if_chat_message(content)
|
chat_message = await check_if_chat_message(content)
|
||||||
if chat_message:
|
if chat_message:
|
||||||
info = await get_info(chat_message['username'])
|
info = await get_info(chat_message["username"])
|
||||||
if info is not None:
|
if info is not None:
|
||||||
await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], info['data']['player']['avatar'])
|
await send_chat_discord(coginstance, chat_message["username"], chat_message["message"], info["data"]["player"]["avatar"])
|
||||||
else:
|
else:
|
||||||
await send_chat_discord(coginstance, chat_message['username'], chat_message['message'], 'https://seafsh.cc/u/j3AzqQ.png')
|
await send_chat_discord(coginstance, chat_message["username"], chat_message["message"], "https://seafsh.cc/u/j3AzqQ.png")
|
||||||
|
|
||||||
join_message = await check_if_join_message(content)
|
join_message = await check_if_join_message(content)
|
||||||
if join_message:
|
if join_message:
|
||||||
if chat_channel is not None:
|
if chat_channel is not None:
|
||||||
if coginstance.bot.embed_requested(chat_channel):
|
if coginstance.bot.embed_requested(chat_channel):
|
||||||
embed, img = await generate_join_leave_embed(coginstance=coginstance, username=join_message,join=True)
|
embed, img = await generate_join_leave_embed(coginstance=coginstance, username=join_message, join=True)
|
||||||
if img:
|
if img:
|
||||||
with open(img, 'rb') as file:
|
with open(img, "rb") as file:
|
||||||
await chat_channel.send(embed=embed, file=file)
|
await chat_channel.send(embed=embed, file=file)
|
||||||
else:
|
else:
|
||||||
await chat_channel.send(embed=embed)
|
await chat_channel.send(embed=embed)
|
||||||
|
@ -93,9 +93,9 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
|
||||||
if leave_message:
|
if leave_message:
|
||||||
if chat_channel is not None:
|
if chat_channel is not None:
|
||||||
if coginstance.bot.embed_requested(chat_channel):
|
if coginstance.bot.embed_requested(chat_channel):
|
||||||
embed, img = await generate_join_leave_embed(coginstance=coginstance, username=leave_message,join=False)
|
embed, img = await generate_join_leave_embed(coginstance=coginstance, username=leave_message, join=False)
|
||||||
if img:
|
if img:
|
||||||
with open(img, 'rb') as file:
|
with open(img, "rb") as file:
|
||||||
await chat_channel.send(embed=embed, file=file)
|
await chat_channel.send(embed=embed, file=file)
|
||||||
else:
|
else:
|
||||||
await chat_channel.send(embed=embed)
|
await chat_channel.send(embed=embed)
|
||||||
|
@ -106,13 +106,13 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
|
||||||
if achievement_message:
|
if achievement_message:
|
||||||
if chat_channel is not None:
|
if chat_channel is not None:
|
||||||
if coginstance.bot.embed_requested(chat_channel):
|
if coginstance.bot.embed_requested(chat_channel):
|
||||||
await chat_channel.send(embed=await generate_achievement_embed(coginstance, achievement_message['username'], achievement_message['achievement'], achievement_message['challenge']))
|
await chat_channel.send(embed=await generate_achievement_embed(coginstance, achievement_message["username"], achievement_message["achievement"], achievement_message["challenge"]))
|
||||||
else:
|
else:
|
||||||
await chat_channel.send(f"{achievement_message['username']} has {'completed the challenge' if achievement_message['challenge'] else 'made the advancement'} {achievement_message['achievement']}")
|
await chat_channel.send(f"{achievement_message['username']} has {'completed the challenge' if achievement_message['challenge'] else 'made the advancement'} {achievement_message['achievement']}")
|
||||||
|
|
||||||
if message['event'] == 'status':
|
if message["event"] == "status":
|
||||||
old_status = await config.current_status()
|
old_status = await config.current_status()
|
||||||
current_status = message['args'][0]
|
current_status = message["args"][0]
|
||||||
if old_status != current_status:
|
if old_status != current_status:
|
||||||
await config.current_status.set(current_status)
|
await config.current_status.set(current_status)
|
||||||
if await config.console_channel() is not None:
|
if await config.console_channel() is not None:
|
||||||
|
@ -120,15 +120,16 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
|
||||||
if console is not None:
|
if console is not None:
|
||||||
await console.send(f"Server status changed! `{current_status}`")
|
await console.send(f"Server status changed! `{current_status}`")
|
||||||
if await config.chat_channel() is not None:
|
if await config.chat_channel() is not None:
|
||||||
if current_status == 'running' and await config.startup_msg() is not None:
|
if current_status == "running" and await config.startup_msg() is not None:
|
||||||
chat = coginstance.bot.get_channel(await config.chat_channel())
|
chat = coginstance.bot.get_channel(await config.chat_channel())
|
||||||
if chat is not None:
|
if chat is not None:
|
||||||
await chat.send(await config.startup_msg())
|
await chat.send(await config.startup_msg())
|
||||||
if current_status == 'stopping' and await config.shutdown_msg() is not None:
|
if current_status == "stopping" and await config.shutdown_msg() is not None:
|
||||||
chat = coginstance.bot.get_channel(await config.chat_channel())
|
chat = coginstance.bot.get_channel(await config.chat_channel())
|
||||||
if chat is not None:
|
if chat is not 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")
|
pterodactyl_keys = await coginstance.bot.get_shared_api_tokens("pterodactyl")
|
||||||
api_key = pterodactyl_keys.get("api_key")
|
api_key = pterodactyl_keys.get("api_key")
|
||||||
|
@ -147,19 +148,22 @@ async def retrieve_websocket_credentials(coginstance: Pterodactyl) -> Optional[d
|
||||||
client = PterodactylClient(base_url, api_key).client
|
client = PterodactylClient(base_url, api_key).client
|
||||||
coginstance.client = client
|
coginstance.client = client
|
||||||
websocket_credentials = client.servers.get_websocket(server_id)
|
websocket_credentials = client.servers.get_websocket(server_id)
|
||||||
logger.debug("""Websocket connection details retrieved:
|
logger.debug(
|
||||||
|
"""Websocket connection details retrieved:
|
||||||
Socket: %s
|
Socket: %s
|
||||||
Token: %s...""",
|
Token: %s...""",
|
||||||
websocket_credentials['data']['socket'],
|
websocket_credentials["data"]["socket"],
|
||||||
websocket_credentials['data']['token'][:20],
|
websocket_credentials["data"]["token"][:20],
|
||||||
)
|
)
|
||||||
return websocket_credentials
|
return websocket_credentials
|
||||||
#NOTE - The token is truncated to prevent it from being logged in its entirety, for security reasons
|
# NOTE - The token is truncated to prevent it from being logged in its entirety, for security reasons
|
||||||
|
|
||||||
|
|
||||||
def remove_ansi_escape_codes(text: str) -> str:
|
def remove_ansi_escape_codes(text: str) -> str:
|
||||||
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
|
||||||
#NOTE - https://chat.openai.com/share/d92f9acf-d776-4fd6-a53f-b14ac15dd540
|
# NOTE - https://chat.openai.com/share/d92f9acf-d776-4fd6-a53f-b14ac15dd540
|
||||||
return ansi_escape.sub('', text)
|
return ansi_escape.sub("", text)
|
||||||
|
|
||||||
|
|
||||||
async def check_if_server_message(text: str) -> Union[bool, str]:
|
async def check_if_server_message(text: str) -> Union[bool, str]:
|
||||||
regex = await config.server_regex()
|
regex = await config.server_regex()
|
||||||
|
@ -169,6 +173,7 @@ async def check_if_server_message(text: str) -> Union[bool, str]:
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def check_if_chat_message(text: str) -> Union[bool, dict]:
|
async def check_if_chat_message(text: str) -> Union[bool, dict]:
|
||||||
regex = await config.chat_regex()
|
regex = await config.chat_regex()
|
||||||
match: Optional[re.Match[str]] = re.match(regex, text)
|
match: Optional[re.Match[str]] = re.match(regex, text)
|
||||||
|
@ -178,6 +183,7 @@ async def check_if_chat_message(text: str) -> Union[bool, dict]:
|
||||||
return groups
|
return groups
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def check_if_join_message(text: str) -> Union[bool, str]:
|
async def check_if_join_message(text: str) -> Union[bool, str]:
|
||||||
regex = await config.join_regex()
|
regex = await config.join_regex()
|
||||||
match: Optional[re.Match[str]] = re.match(regex, text)
|
match: Optional[re.Match[str]] = re.match(regex, text)
|
||||||
|
@ -186,6 +192,7 @@ async def check_if_join_message(text: str) -> Union[bool, str]:
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def check_if_leave_message(text: str) -> Union[bool, str]:
|
async def check_if_leave_message(text: str) -> Union[bool, str]:
|
||||||
regex = await config.leave_regex()
|
regex = await config.leave_regex()
|
||||||
match: Optional[re.Match[str]] = re.match(regex, text)
|
match: Optional[re.Match[str]] = re.match(regex, text)
|
||||||
|
@ -194,6 +201,7 @@ async def check_if_leave_message(text: str) -> Union[bool, str]:
|
||||||
return match.group(1)
|
return match.group(1)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def check_if_achievement_message(text: str) -> Union[bool, dict]:
|
async def check_if_achievement_message(text: str) -> Union[bool, dict]:
|
||||||
regex = await config.achievement_regex()
|
regex = await config.achievement_regex()
|
||||||
match: Optional[re.Match[str]] = re.match(regex, text)
|
match: Optional[re.Match[str]] = re.match(regex, text)
|
||||||
|
@ -207,6 +215,7 @@ async def check_if_achievement_message(text: str) -> Union[bool, dict]:
|
||||||
return groups
|
return groups
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def get_info(username: str) -> Optional[dict]:
|
async def get_info(username: str) -> Optional[dict]:
|
||||||
logger.verbose("Retrieving player info for %s", username)
|
logger.verbose("Retrieving player info for %s", username)
|
||||||
endpoint = await config.api_endpoint()
|
endpoint = await config.api_endpoint()
|
||||||
|
@ -218,6 +227,7 @@ async def get_info(username: str) -> Optional[dict]:
|
||||||
logger.warning("Failed to retrieve player info for %s: %s", username, response.status)
|
logger.warning("Failed to retrieve player info for %s: %s", username, response.status)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
async def send_chat_discord(coginstance: Pterodactyl, username: str, message: str, avatar_url: str) -> None:
|
async def send_chat_discord(coginstance: Pterodactyl, username: str, message: str, avatar_url: str) -> None:
|
||||||
logger.trace("Sending chat message to Discord")
|
logger.trace("Sending chat message to Discord")
|
||||||
channel = coginstance.bot.get_channel(await config.chat_channel())
|
channel = coginstance.bot.get_channel(await config.chat_channel())
|
||||||
|
@ -231,6 +241,7 @@ async def send_chat_discord(coginstance: Pterodactyl, username: str, message: st
|
||||||
else:
|
else:
|
||||||
logger.warning("Chat channel not set. Skipping sending chat message to Discord")
|
logger.warning("Chat channel not set. Skipping sending chat message to Discord")
|
||||||
|
|
||||||
|
|
||||||
async def generate_join_leave_embed(coginstance: Pterodactyl, username: str, join: bool) -> Tuple[discord.Embed, Optional[Union[str, Path]]]:
|
async def generate_join_leave_embed(coginstance: Pterodactyl, username: str, join: bool) -> Tuple[discord.Embed, Optional[Union[str, Path]]]:
|
||||||
embed = discord.Embed()
|
embed = discord.Embed()
|
||||||
embed.color = discord.Color.green() if join else discord.Color.red()
|
embed.color = discord.Color.green() if join else discord.Color.red()
|
||||||
|
@ -238,29 +249,32 @@ async def generate_join_leave_embed(coginstance: Pterodactyl, username: str, joi
|
||||||
info = await get_info(username)
|
info = await get_info(username)
|
||||||
if info:
|
if info:
|
||||||
img = None
|
img = None
|
||||||
embed.set_author(name=username, icon_url=info['data']['player']['avatar'])
|
embed.set_author(name=username, icon_url=info["data"]["player"]["avatar"])
|
||||||
else:
|
else:
|
||||||
img = bundled_data_path(coginstance) / "unknown.png"
|
img = bundled_data_path(coginstance) / "unknown.png"
|
||||||
embed.set_author(name=username, icon_url='attachment://unknown.png')
|
embed.set_author(name=username, icon_url="attachment://unknown.png")
|
||||||
embed.timestamp = discord.utils.utcnow()
|
embed.timestamp = discord.utils.utcnow()
|
||||||
return embed, img
|
return embed, img
|
||||||
|
|
||||||
|
|
||||||
async def generate_achievement_embed(coginstance: Pterodactyl, username: str, achievement: str, challenge: bool) -> Tuple[discord.Embed, Optional[Union[str, Path]]]:
|
async def generate_achievement_embed(coginstance: Pterodactyl, username: str, achievement: str, challenge: bool) -> Tuple[discord.Embed, Optional[Union[str, Path]]]:
|
||||||
embed = discord.Embed()
|
embed = discord.Embed()
|
||||||
embed.color = discord.Color.from_str('#a800a7') if challenge else discord.Color.from_str('#54fb54')
|
embed.color = discord.Color.from_str("#a800a7") if challenge else discord.Color.from_str("#54fb54")
|
||||||
embed.description = f"{bold(username)} has {'completed the challenge' if challenge else 'made the advancement'} {bold(achievement)}"
|
embed.description = f"{bold(username)} has {'completed the challenge' if challenge else 'made the advancement'} {bold(achievement)}"
|
||||||
info = await get_info(username)
|
info = await get_info(username)
|
||||||
if info:
|
if info:
|
||||||
img = None
|
img = None
|
||||||
embed.set_author(name=username, icon_url=info['data']['player']['avatar'])
|
embed.set_author(name=username, icon_url=info["data"]["player"]["avatar"])
|
||||||
else:
|
else:
|
||||||
img = bundled_data_path(coginstance) / "unknown.png"
|
img = bundled_data_path(coginstance) / "unknown.png"
|
||||||
embed.set_author(name=username, icon_url='attachment://unknown.png')
|
embed.set_author(name=username, icon_url="attachment://unknown.png")
|
||||||
embed.timestamp = discord.utils.utcnow()
|
embed.timestamp = discord.utils.utcnow()
|
||||||
return embed, img
|
return embed, img
|
||||||
|
|
||||||
|
|
||||||
def mask_ip(string: str) -> str:
|
def mask_ip(string: str) -> str:
|
||||||
def check(match: re.Match[str]):
|
def check(match: re.Match[str]):
|
||||||
ip = match.group(0)
|
ip = match.group(0)
|
||||||
return '.'.join(r'\*' * len(octet) for octet in ip.split('.'))
|
return ".".join(r"\*" * len(octet) for octet in ip.split("."))
|
||||||
return re.sub(r'\b(?:\d{1,3}\.){3}\d{1,3}\b', check, string)
|
|
||||||
|
return re.sub(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", check, string)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue