From bc0f71d5bea799762651af8f7befa0d32534b64f Mon Sep 17 00:00:00 2001 From: Valkyrie Date: Sat, 17 May 2025 17:49:03 +0100 Subject: [PATCH] Cleanup parsing.py --- bot/parsing.py | 82 ++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/bot/parsing.py b/bot/parsing.py index 9c6cdcf..a4895b8 100644 --- a/bot/parsing.py +++ b/bot/parsing.py @@ -1,29 +1,28 @@ import random, re from gacha_response import gacha_response - def parse_notification(notification,client): + '''Oarses any notifications received by the bot and sends any commands to + gacha_response()''' - # We get the type of notification to filter the ones that we actually want to parse + # We get the type of notification to filter the ones that we actually want + # to parse notif_type = notification.get("type") - if not((notif_type == "mention") or (notif_type == "reply")): - - return # Ignora todo lo que no sea una mención - - # We want the visibility to be related to the type that was received (so if people don't want to dump a bunch of notes on home they don't have to) + if not notif_type in ('mention', 'reply'): + return # Ignore anything that isn't a mention + # We want the visibility to be related to the type that was received (so if + # people don't want to dump a bunch of notes on home they don't have to) visibility = notification["note"]["visibility"] - if visibility == "specified": - visibility = "specified" - else: + if visibility != "specified": visibility = "home" - - + # Get the full Activitypub ID of the user user = notification.get("user", {}) username = user.get("username", "unknown") host = user.get("host") + # Local users may not have a hostname attached full_user = f"@{username}" if not host else f"@{username}@{host}" note_obj = notification.get("note", {}) @@ -32,38 +31,41 @@ def parse_notification(notification,client): note = note_text.strip().lower() if note_text else "" + # Will want to move bot name to a config file or find a way to derermine + # AP ID at runtime waifumelon_variants = [ "@waifumelon", # simple mention "@waifumelon@mai.waifuism.life", # fully qualified ] - if any(variant in note for variant in waifumelon_variants): - # Find command and arguments after the mention - # Removes all mentions (regex = mentions that start with @ and may contain @domain) - cleaned_text = re.sub(r"@\w+(?:@\S+)?", "", note).strip() - parts = cleaned_text.split() - print(cleaned_text) - print(parts) + # Make sure the notification text explicitly mentions the bot + if not any(variant in note for variant in waifumelon_variants): + return - command = parts[0].lower() if parts else None - arguments = parts[1:] if len(parts) > 1 else [] + # Find command and arguments after the mention + # Removes all mentions (regex = mentions that start with @ and may contain @domain) + cleaned_text = re.sub(r"@\w+(?:@\S+)?", "", note).strip() + parts = cleaned_text.split() + + command = parts[0].lower() if parts else None + arguments = parts[1:] if len(parts) > 1 else [] + + # TODO: move response generation to a different function + response = gacha_response(command.lower(),full_user, arguments, note_obj) + if not response: + return + + if isinstance(response, str): + client.notes_create( + text=response, + reply_id=note_id, + visibility=visibility + ) else: - command = None - if command: - response = gacha_response(command.lower(),full_user, arguments, note_obj) - if response: - if type(response) == str: - client.notes_create( - text=response, - reply_id=note_id, - visibility=visibility - ) - else: - client.notes_create( - text=response[0], - reply_id=note_id, - visibility=visibility, - file_ids=response[1] - #visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately - - ) \ No newline at end of file + client.notes_create( + text=response[0], + reply_id=note_id, + visibility=visibility, + file_ids=response[1] + #visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately + )