From 7e03696e10c0be748af9c0537814409fa84ffb36 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 1 Mar 2024 00:46:51 -0500 Subject: [PATCH] feat(pterodactyl): added achievements --- pterodactyl/config.py | 1 + pterodactyl/pterodactyl.py | 36 ++++++++++++++++++++++++++++++++++++ pterodactyl/websocket.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/pterodactyl/config.py b/pterodactyl/config.py index d4d3d96..b1c49c2 100644 --- a/pterodactyl/config.py +++ b/pterodactyl/config.py @@ -15,6 +15,7 @@ def register_config(config_obj: Config) -> None: server_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: \[Not Secure\] \[.*\] (.*)", join_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: ([^<\n]+) joined the game$", leave_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: ([^<\n]+) left the game$", + achievement_regex=r"^\[\d{2}:\d{2}:\d{2} INFO\]: (.*) has (made the advancement|completed the challenge) \[(.*)\]$", chat_command='tellraw @a ["",{"text":".$U ","color":".$C"},{"text":" (DISCORD): ","color":"blue"},{"text":".$M","color":"white"}]', api_endpoint="minecraft", chat_channel=None, diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index d644331..c6e40b3 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -175,6 +175,42 @@ class Pterodactyl(commands.Cog): await config.server_regex.set(regex) await ctx.send(f"Server regex set to:\n{box(regex, 'regex')}") + @pterodactyl_config_regex.command(name = "join") + async def pterodactyl_config_regex_join(self, ctx: commands.Context, *, regex: str = None) -> None: + """Set the regex pattern to match join messages on the server. + + See [documentation]() for more information.""" + #TODO - fix this link + if regex is None: + regex = await config.join_regex() + return await ctx.send(f"Join regex is currently set to:\n{box(regex, 'regex')}") + await config.join_regex.set(regex) + await ctx.send(f"Join regex set to:\n{box(regex, 'regex')}") + + @pterodactyl_config_regex.command(name = "leave") + async def pterodactyl_config_regex_leave(self, ctx: commands.Context, *, regex: str = None) -> None: + """Set the regex pattern to match leave messages on the server. + + See [documentation]() for more information.""" + #TODO - fix this link + if regex is None: + regex = await config.leave_regex() + return await ctx.send(f"Leave regex is currently set to:\n{box(regex, 'regex')}") + await config.leave_regex.set(regex) + await ctx.send(f"Leave regex set to:\n{box(regex, 'regex')}") + + @pterodactyl_config_regex.command(name = "achievement") + async def pterodactyl_config_regex_achievement(self, ctx: commands.Context, *, regex: str = None) -> None: + """Set the regex pattern to match achievement messages on the server. + + See [documentation]() for more information.""" + #TODO - fix this link + if regex is None: + regex = await config.achievement_regex() + return await ctx.send(f"Achievement regex is currently set to:\n{box(regex, 'regex')}") + await config.achievement_regex.set(regex) + await ctx.send(f"Achievement regex set to:\n{box(regex, 'regex')}") + @pterodactyl_config.group(name = "messages", aliases = ['msg', 'msgs', 'message']) async def pterodactyl_config_messages(self, ctx: commands.Context): """Configure message settings.""" diff --git a/pterodactyl/websocket.py b/pterodactyl/websocket.py index dd2c608..e23651c 100644 --- a/pterodactyl/websocket.py +++ b/pterodactyl/websocket.py @@ -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