forked from waifu/kemoverse
Cleanup parsing.py
This commit is contained in:
parent
b8493440bd
commit
bc0f71d5be
1 changed files with 42 additions and 40 deletions
|
@ -1,29 +1,28 @@
|
||||||
import random, re
|
import random, re
|
||||||
from gacha_response import gacha_response
|
from gacha_response import gacha_response
|
||||||
|
|
||||||
|
|
||||||
def parse_notification(notification,client):
|
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")
|
notif_type = notification.get("type")
|
||||||
if not((notif_type == "mention") or (notif_type == "reply")):
|
if not notif_type in ('mention', 'reply'):
|
||||||
|
return # Ignore anything that isn't a mention
|
||||||
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)
|
|
||||||
|
|
||||||
|
# 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"]
|
visibility = notification["note"]["visibility"]
|
||||||
if visibility == "specified":
|
if visibility != "specified":
|
||||||
visibility = "specified"
|
|
||||||
else:
|
|
||||||
visibility = "home"
|
visibility = "home"
|
||||||
|
|
||||||
|
# Get the full Activitypub ID of the user
|
||||||
|
|
||||||
user = notification.get("user", {})
|
user = notification.get("user", {})
|
||||||
username = user.get("username", "unknown")
|
username = user.get("username", "unknown")
|
||||||
host = user.get("host")
|
host = user.get("host")
|
||||||
|
# Local users may not have a hostname attached
|
||||||
full_user = f"@{username}" if not host else f"@{username}@{host}"
|
full_user = f"@{username}" if not host else f"@{username}@{host}"
|
||||||
|
|
||||||
note_obj = notification.get("note", {})
|
note_obj = notification.get("note", {})
|
||||||
|
@ -32,38 +31,41 @@ def parse_notification(notification,client):
|
||||||
|
|
||||||
note = note_text.strip().lower() if note_text else ""
|
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_variants = [
|
||||||
"@waifumelon", # simple mention
|
"@waifumelon", # simple mention
|
||||||
"@waifumelon@mai.waifuism.life", # fully qualified
|
"@waifumelon@mai.waifuism.life", # fully qualified
|
||||||
]
|
]
|
||||||
|
|
||||||
if any(variant in note for variant in waifumelon_variants):
|
# Make sure the notification text explicitly mentions the bot
|
||||||
# Find command and arguments after the mention
|
if not any(variant in note for variant in waifumelon_variants):
|
||||||
# Removes all mentions (regex = mentions that start with @ and may contain @domain)
|
return
|
||||||
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
|
# Find command and arguments after the mention
|
||||||
arguments = parts[1:] if len(parts) > 1 else []
|
# 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:
|
else:
|
||||||
command = None
|
client.notes_create(
|
||||||
if command:
|
text=response[0],
|
||||||
response = gacha_response(command.lower(),full_user, arguments, note_obj)
|
reply_id=note_id,
|
||||||
if response:
|
visibility=visibility,
|
||||||
if type(response) == str:
|
file_ids=response[1]
|
||||||
client.notes_create(
|
#visible_user_ids=[] #todo: write actual visible users ids so pleromers can use the bot privately
|
||||||
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
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue