61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
|
|
import re
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_ship(ship: str) -> str:
|
||
|
|
ship = ship.strip().lower()
|
||
|
|
if not ship:
|
||
|
|
return ""
|
||
|
|
if not ship.startswith("~"):
|
||
|
|
ship = f"~{ship}"
|
||
|
|
return ship
|
||
|
|
|
||
|
|
|
||
|
|
def is_bot_mentioned(text: str, bot_ship: str, bot_nickname: str | None = None) -> bool:
|
||
|
|
patterns = []
|
||
|
|
|
||
|
|
escaped_ship = re.escape(normalize_ship(bot_ship))
|
||
|
|
patterns.append(rf"(?<!\w){escaped_ship}(?!\w)")
|
||
|
|
|
||
|
|
patterns.append(r"(?<!\w)@all(?!\w)")
|
||
|
|
|
||
|
|
if bot_nickname:
|
||
|
|
escaped_nick = re.escape(bot_nickname)
|
||
|
|
patterns.append(rf"(?<!\w){escaped_nick}(?!\w)")
|
||
|
|
|
||
|
|
combined = "|".join(patterns)
|
||
|
|
return bool(re.search(combined, text, re.IGNORECASE))
|
||
|
|
|
||
|
|
|
||
|
|
def is_admin_mentioned(text: str) -> bool:
|
||
|
|
return bool(re.search(r"(?<!\w)@admin(?!\w)", text, re.IGNORECASE))
|
||
|
|
|
||
|
|
|
||
|
|
def extract_dm_partner_ship(whom: dict | str) -> str:
|
||
|
|
raw = ""
|
||
|
|
if isinstance(whom, str):
|
||
|
|
raw = whom
|
||
|
|
elif isinstance(whom, dict) and "ship" in whom:
|
||
|
|
raw = str(whom["ship"])
|
||
|
|
|
||
|
|
normalized = raw.strip().lstrip("~")
|
||
|
|
if re.match(r"^[a-z-]+$", normalized, re.IGNORECASE):
|
||
|
|
return f"~{normalized}"
|
||
|
|
return ""
|
||
|
|
|
||
|
|
|
||
|
|
def is_valid_ship(ship: str) -> bool:
|
||
|
|
normalized = ship.strip().lstrip("~")
|
||
|
|
return bool(re.match(r"^[a-z-]+$", normalized, re.IGNORECASE))
|
||
|
|
|
||
|
|
|
||
|
|
def is_owner(sender_ship: str, owner_ship: str | None) -> bool:
|
||
|
|
if not owner_ship:
|
||
|
|
return False
|
||
|
|
return normalize_ship(sender_ship) == normalize_ship(owner_ship)
|
||
|
|
|
||
|
|
|
||
|
|
def format_at_ud(msg_id: str) -> str:
|
||
|
|
parts = msg_id.split("/")
|
||
|
|
if len(parts) == 2:
|
||
|
|
return msg_id
|
||
|
|
return f"~zod/{msg_id}"
|