migration vers new components V2, et ajout de diverse fonctions !
This commit is contained in:
parent
3590cb748f
commit
ba9e297cb8
14 changed files with 851 additions and 371 deletions
|
|
@ -106,21 +106,42 @@ class AdvancedLogger:
|
|||
|
||||
def check_previous_roles(self, member_id: int) -> Optional[Dict]:
|
||||
"""Check if member has previous role data when rejoining"""
|
||||
# This one stays synchronous for now as it's called in on_member_join and needs immediate result
|
||||
# but since it only reads ONE file, it's less problematic than writing to cumulative logs.
|
||||
# Format update: it now needs to read NDJSON.
|
||||
member_log_file = f"{self.member_logs_dir}/{member_id}.json"
|
||||
|
||||
if not os.path.exists(member_log_file):
|
||||
return None
|
||||
|
||||
try:
|
||||
logs = []
|
||||
with open(member_log_file, 'r', encoding='utf-8') as f:
|
||||
# Read line by line for NDJSON
|
||||
logs = [json.loads(line) for line in f if line.strip()]
|
||||
content = f.read().strip()
|
||||
if not content:
|
||||
return None
|
||||
|
||||
# Handle legacy JSON list format
|
||||
if content.startswith('['):
|
||||
try:
|
||||
logs = json.loads(content)
|
||||
except json.JSONDecodeError:
|
||||
# Fallback: try to strip brackets and commas
|
||||
content_clean = content.strip('[]').strip()
|
||||
# This is getting complex, let's just try line-by-line fallback
|
||||
pass
|
||||
|
||||
# If not a list or failed list parse, try NDJSON line-by-line
|
||||
if not logs:
|
||||
for line in content.splitlines():
|
||||
line = line.strip()
|
||||
if not line: continue
|
||||
# Strip trailing/leading commas in case of semi-corrupt legacy conversion
|
||||
line = line.strip(',')
|
||||
try:
|
||||
logs.append(json.loads(line))
|
||||
except json.JSONDecodeError:
|
||||
continue # Skip corrupt lines
|
||||
|
||||
# Find the most recent leave event
|
||||
leave_events = [log for log in logs if log.get("event_type") == "member_leave"]
|
||||
leave_events = [log for log in logs if isinstance(log, dict) and log.get("event_type") == "member_leave"]
|
||||
if leave_events:
|
||||
return leave_events[-1].get("roles", {})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue