import random, re import config 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 notif_type = notification.get("type") 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 = "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", {}) note_text = note_obj.get("text") note_id = note_obj.get("id") note = note_text.strip().lower() if note_text else "" # Check for both short and fully-qualified name mentions username_variants = [ config.USER, f'@{config.USER.split('@')[1]}' ] # Make sure the notification text explicitly mentions the bot if not any(variant in note for variant in username_variants): return # 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: 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 )