forked from aliveriver/DLUT-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_utils.py
More file actions
52 lines (41 loc) · 1.68 KB
/
command_utils.py
File metadata and controls
52 lines (41 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from astrbot.api.event import AstrMessageEvent
from rss_service import Notice
def extract_command_args(event: AstrMessageEvent, command_name: str) -> str:
command_name = command_name.lower()
for candidate in event_text_candidates(event):
normalized = " ".join(candidate.strip().split())
if not normalized:
continue
lowered = normalized.lower()
prefixes = [
f"/dut_notice {command_name}",
f"dut_notice {command_name}",
f"{command_name}",
]
for prefix in prefixes:
if lowered == prefix:
return ""
if lowered.startswith(prefix + " "):
return normalized[len(prefix) :].strip()
if "dut_notice" not in lowered and command_name not in lowered:
return normalized
return ""
def event_text_candidates(event: AstrMessageEvent) -> list[str]:
candidates: list[str] = []
for attr in ("message_str", "message_text", "raw_message", "text"):
value = getattr(event, attr, None)
if isinstance(value, str) and value.strip():
candidates.append(value)
message_obj = getattr(event, "message_obj", None)
if message_obj is not None:
for attr in ("message_str", "text"):
value = getattr(message_obj, attr, None)
if isinstance(value, str) and value.strip():
candidates.append(value)
return candidates
def format_latest_lines(title: str, notices: list[Notice]) -> str:
lines = [title]
for item in notices:
lines.append(f"- [{item['source']}] {item['date']} | {item['title']}")
lines.append(item["link"])
return "\n".join(lines)