83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
|
|
"""Action log parsing for context building."""
|
||
|
|
|
||
|
|
import os
|
||
|
|
import json
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger('Beta')
|
||
|
|
|
||
|
|
|
||
|
|
def get_recent_logs(limit: int = 15) -> str:
|
||
|
|
"""Read and format recent action logs for context."""
|
||
|
|
log_path = "data/actions.log"
|
||
|
|
if not os.path.exists(log_path):
|
||
|
|
return ""
|
||
|
|
|
||
|
|
try:
|
||
|
|
with open(log_path, 'r', encoding='utf-8') as f:
|
||
|
|
lines = f.readlines()
|
||
|
|
last_lines = lines[-limit:]
|
||
|
|
|
||
|
|
formatted_logs = []
|
||
|
|
for line in last_lines:
|
||
|
|
if "ACTION:" not in line:
|
||
|
|
continue
|
||
|
|
try:
|
||
|
|
parts = line.split("ACTION:", 1)
|
||
|
|
if len(parts) <= 1:
|
||
|
|
continue
|
||
|
|
json_str = parts[1].strip()
|
||
|
|
data = json.loads(json_str)
|
||
|
|
|
||
|
|
timestamp = data.get("timestamp", "").split("T")[1].split(".")[0]
|
||
|
|
action = data.get("action", "UNKNOWN")
|
||
|
|
user = data.get("user", "System")
|
||
|
|
params = data.get("params", {})
|
||
|
|
success = data.get("success", True)
|
||
|
|
|
||
|
|
details = _format_action_details(action, params)
|
||
|
|
status = "\u2705" if success else "\u274c"
|
||
|
|
formatted_logs.append(f"[{timestamp}] {status} {action} {details} by {user}")
|
||
|
|
except Exception:
|
||
|
|
continue
|
||
|
|
|
||
|
|
if not formatted_logs:
|
||
|
|
return ""
|
||
|
|
|
||
|
|
return "\n[M\u00c9MOIRE DES ACTIONS R\u00c9CENTES (V\u00c9RIT\u00c9)]\n" + "\n".join(formatted_logs) + "\n"
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Error reading actions log: {e}")
|
||
|
|
return ""
|
||
|
|
|
||
|
|
|
||
|
|
def _format_action_details(action: str, params: dict) -> str:
|
||
|
|
"""Format action-specific details for log display."""
|
||
|
|
if action == 'SEND_MESSAGE':
|
||
|
|
target = params.get('channel_id') or params.get('user_id')
|
||
|
|
content = params.get('content', '')[:20]
|
||
|
|
return f"(to: {target}, content: {content}...)"
|
||
|
|
|
||
|
|
if 'CHANNEL' in action:
|
||
|
|
items = params.get('channels', [])
|
||
|
|
if items:
|
||
|
|
names = [i.get('channel_name', 'unknown') for i in items]
|
||
|
|
return f"(channels: {', '.join(names)})"
|
||
|
|
|
||
|
|
if 'ROLE' in action:
|
||
|
|
items = params.get('roles', [])
|
||
|
|
if items:
|
||
|
|
names = [i.get('role_name', 'unknown') for i in items]
|
||
|
|
return f"(roles: {', '.join(names)})"
|
||
|
|
|
||
|
|
if action in ('KICK', 'BAN', 'WARN', 'MUTE', 'TIMEOUT'):
|
||
|
|
target = params.get('user_id', 'unknown')
|
||
|
|
reason = params.get('reason', 'no reason')
|
||
|
|
return f"(target: {target}, reason: {reason})"
|
||
|
|
|
||
|
|
if action == 'PURGE':
|
||
|
|
purges = params.get('purges', [])
|
||
|
|
if purges:
|
||
|
|
p = purges[0]
|
||
|
|
return f"(channel: {p.get('channel_name')}, amount: {p.get('amount')})"
|
||
|
|
|
||
|
|
return ""
|