You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
kemoverse/bot/parsing.py

69 lines
2.3 KiB

import random, re
from gacha_response import gacha_response
def parse_notification(notification,client):
# 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)
visibility = notification["note"]["visibility"]
if visibility == "specified":
visibility = "specified"
else:
visibility = "home"
user = notification.get("user", {})
username = user.get("username", "unknown")
host = user.get("host")
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 ""
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)
command = parts[0].lower() if parts else None
arguments = parts[1:] if len(parts) > 1 else []
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
)