fix(pterodactyl): fixed join and leave listeners throwing errors
Some checks failed
Actions / Build Documentation (MkDocs) (push) Successful in 31s
Actions / Lint Code (Ruff & Pylint) (push) Failing after 56s

This commit is contained in:
Seaswimmer 2024-08-26 19:43:57 -04:00
parent 1244d5a941
commit ad0bb7cf00
Signed by: cswimr
GPG key ID: 3813315477F26F82
2 changed files with 13 additions and 13 deletions

View file

@ -22,7 +22,7 @@ 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.2" __version__ = "2.0.3"
__documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/" __documentation__ = "https://seacogs.coastalcommits.com/pterodactyl/"
def __init__(self, bot: Red): def __init__(self, bot: Red):

View file

@ -80,7 +80,7 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
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=chat_message['username'],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)
@ -93,7 +93,7 @@ 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=chat_message['username'],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)
@ -165,7 +165,7 @@ async def check_if_server_message(text: str) -> Union[bool, str]:
regex = await config.server_regex() regex = await config.server_regex()
match: Optional[re.Match[str]] = re.match(regex, text) match: Optional[re.Match[str]] = re.match(regex, text)
if match: if match:
logger.debug("Message is a server message") logger.trace("Message is a server message")
return match.group(1) return match.group(1)
return False return False
@ -174,7 +174,7 @@ async def check_if_chat_message(text: str) -> Union[bool, dict]:
match: Optional[re.Match[str]] = re.match(regex, text) match: Optional[re.Match[str]] = re.match(regex, text)
if match: if match:
groups = {"username": match.group(1), "message": match.group(2)} groups = {"username": match.group(1), "message": match.group(2)}
logger.debug("Message is a chat message\n%s", json.dumps(groups)) logger.trace("Message is a chat message\n%s", json.dumps(groups))
return groups return groups
return False return False
@ -182,7 +182,7 @@ 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)
if match: if match:
logger.debug("Message is a join message") logger.trace("Message is a join message")
return match.group(1) return match.group(1)
return False return False
@ -190,7 +190,7 @@ 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)
if match: if match:
logger.debug("Message is a leave message") logger.trace("Message is a leave message")
return match.group(1) return match.group(1)
return False return False
@ -203,23 +203,23 @@ async def check_if_achievement_message(text: str) -> Union[bool, dict]:
groups["challenge"] = True groups["challenge"] = True
else: else:
groups["challenge"] = False groups["challenge"] = False
logger.debug("Message is an achievement message") logger.trace("Message is an achievement message")
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.debug("Retrieving player info for %s", username) logger.verbose("Retrieving player info for %s", username)
endpoint = await config.api_endpoint() endpoint = await config.api_endpoint()
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(f"https://playerdb.co/api/player/{endpoint}/{username}") as response: async with session.get(f"https://playerdb.co/api/player/{endpoint}/{username}") as response:
if response.status == 200: if response.status == 200:
logger.debug("Player info retrieved for %s", username) logger.verbose("Player info retrieved for %s", username)
return await response.json() return await response.json()
logger.error("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.debug("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())
if channel is not None: if channel is not None:
webhooks = await channel.webhooks() webhooks = await channel.webhooks()
@ -227,7 +227,7 @@ async def send_chat_discord(coginstance: Pterodactyl, username: str, message: st
if webhook is None: if webhook is None:
webhook = await channel.create_webhook(name="Pterodactyl Chat") webhook = await channel.create_webhook(name="Pterodactyl Chat")
await webhook.send(content=message, username=username, avatar_url=avatar_url, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=True)) await webhook.send(content=message, username=username, avatar_url=avatar_url, allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=True))
logger.debug("Chat message sent to Discord") logger.trace("Chat message sent to Discord")
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")