1
0
Fork 0
forked from waifu/kemoverse
kemoverse/bot/parsing.py
2025-06-01 12:24:53 +01:00

52 lines
1.5 KiB
Python

import re
from typing import Dict, Any
import misskey
import config
from custom_types import ParsedNotification
def parse_notification(
notification: Dict[str, Any],
client: misskey.Misskey) -> ParsedNotification | None:
'''Parses any notifications received by the bot'''
# 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 None
# 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 []
return {
'author': full_user,
'command': command,
'arguments': arguments,
'note_obj': note_obj
}