feat(pterodactyl): added achievements
Some checks failed
Actions / Lint Code (Ruff & Pylint) (pull_request) Failing after 20s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 21s

This commit is contained in:
SeaswimmerTheFsh 2024-03-01 00:46:51 -05:00
parent 8954df4c1d
commit 7e03696e10
Signed by: cswimr
GPG key ID: B8953EC01E5C4063
3 changed files with 73 additions and 0 deletions

View file

@ -85,6 +85,15 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
else:
await channel.send(f"{leave_message} left the game")
achievement_message = await check_if_achievement_message(content)
if achievement_message:
channel = coginstance.bot.get_channel(await config.chat_channel())
if channel is not None:
if coginstance.bot.embed_requested(channel):
await channel.send(embed=await generate_achievement_embed(achievement_message['username'], achievement_message['achievement'], achievement_message['challenge']))
else:
await channel.send(f"{achievement_message['username']} has {'completed the challenge' if achievement_message['challenge'] else 'made the advancement'} {achievement_message['achievement']}")
if json.loads(message)['event'] == 'status':
old_status = await config.current_status()
current_status = json.loads(message)['args'][0]
@ -172,6 +181,21 @@ async def check_if_leave_message(text: str) -> Union[bool, str]:
logger.debug("Message is not a leave message")
return False
async def check_if_achievement_message(text: str) -> Union[bool, dict]:
logger.debug("Checking if message is an achievement message")
regex = await config.achievement_regex()
match: Optional[re.Match[str]] = re.match(regex, text)
if match:
groups = {"username": match.group(1), "achievement": match.group(3)}
if match.group(2) == "completed the challenge":
groups["challenge"] = True
else:
groups["challenge"] = False
logger.debug("Message is an achievement message\n%s", json.dumps(groups))
return groups
logger.debug("Message is not an achievement message")
return False
async def get_info(username: str) -> Optional[dict]:
logger.debug("Retrieving player info for %s", username)
endpoint = await config.api_endpoint()
@ -207,3 +231,15 @@ async def generate_join_leave_embed(username: str, join: bool) -> discord.Embed:
embed.set_author(name=username, icon_url='https://seafsh.cc/u/j3AzqQ.png')
embed.timestamp = discord.utils.utcnow()
return embed
async def generate_achievement_embed(username: str, achievement: str, challenge: bool) -> discord.Embed:
embed = discord.Embed()
embed.color = discord.Color.dark_purple() if challenge else discord.Color.brand_green()
embed.description = f"{username} has {'completed the challenge' if challenge else 'made the advancement'} {achievement}"
info = await get_info(username)
if info:
embed.set_author(name=username, icon_url=info['data']['player']['avatar'])
else:
embed.set_author(name=username, icon_url='https://seafsh.cc/u/j3AzqQ.png')
embed.timestamp = discord.utils.utcnow()
return embed