From fa4a54d95f91f4db67df4353b60cf115ae1a081a Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Mon, 22 Jun 2026 17:56:50 +0200 Subject: [PATCH] modified: bot.py --- bot.py | 104 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 25 deletions(-) diff --git a/bot.py b/bot.py index a9e919c..1c5d223 100644 --- a/bot.py +++ b/bot.py @@ -4427,67 +4427,121 @@ def create_mutes_table(): if connection: close_database_connection(connection) -async def save_mute_to_database(user_id, guild_id, moderator_id, reason, duration, start_time, end_time, - process_uuid=None, channel_id=None, mute_role_id=None, message_data=None, message_id=None): - """Saves individual mute records to the database with optional message data and context""" +async def save_mute_to_database(user_id, guild_id, moderator_id, reason, duration, start_time, end_time, + process_uuid=None, channel_id=None, mute_role_id=None, message_data=None, message_id=None): + """Saves individual mute records to the database with optional message data and context. + Attachments are stored as local file paths instead of Discord CDN links.""" connection = None cursor = None try: connection = connect_to_database() cursor = connection.cursor() - - # Extract message data if provided + message_content = None message_attachments = None message_author_id = None message_channel_id = None context_messages = None - + + def extract_local_attachments(raw_attachments): + """ + Converts raw attachment data to local-path-only format. + Strips Discord CDN URLs and only keeps locally saved file info. + raw_attachments can be a JSON string or a list of dicts. + """ + if not raw_attachments: + return None + + if isinstance(raw_attachments, str): + try: + attachments_list = json.loads(raw_attachments) + except Exception: + return raw_attachments # Return as-is if not parseable + else: + attachments_list = raw_attachments + + if not isinstance(attachments_list, list): + return None + + cleaned = [] + for att in attachments_list: + if not isinstance(att, dict): + continue + + entry = { + "filename": att.get("filename", "unknown"), + "content_type": att.get("content_type", "application/octet-stream"), + "size": att.get("size"), + "saved_permanently": att.get("saved_permanently", False) + } + + # Only store local_path — no Discord CDN URLs + local_path = att.get("local_path") + if local_path: + entry["local_path"] = local_path + else: + # No local file saved — note it but don't store the expiring URL + entry["local_path"] = None + entry["saved_permanently"] = False + + # Optionally keep the original filename/attachment ID for reference + if att.get("attachment_id"): + entry["attachment_id"] = att.get("attachment_id") + + cleaned.append(entry) + + return json.dumps(cleaned) if cleaned else None + if message_data: if isinstance(message_data, dict) and "main_message" in message_data: # New format with context main_msg = message_data.get("main_message", {}) message_content = main_msg.get("content") - message_attachments = main_msg.get("attachments") message_author_id = main_msg.get("author_id") message_channel_id = main_msg.get("channel_id") - context_messages = json.dumps(message_data.get("context_messages", [])) + + # Use local paths only, not Discord URLs + message_attachments = extract_local_attachments(main_msg.get("attachments")) + + # Also clean attachments in context_messages before saving + raw_context = message_data.get("context_messages", []) + cleaned_context = [] + for ctx_msg in raw_context: + ctx_copy = dict(ctx_msg) + ctx_copy["attachments"] = extract_local_attachments(ctx_msg.get("attachments")) + cleaned_context.append(ctx_copy) + context_messages = json.dumps(cleaned_context) + else: # Old format or simple dict message_content = message_data.get("content") - message_attachments = message_data.get("attachments") message_author_id = message_data.get("author_id") message_channel_id = message_data.get("channel_id") - - # Convert JSON fields - if message_attachments and isinstance(message_attachments, str): - # Already JSON string - pass - elif message_attachments: - # Convert to JSON string - message_attachments = json.dumps(message_attachments) - + + # Use local paths only + message_attachments = extract_local_attachments(message_data.get("attachments")) + insert_query = """ INSERT INTO user_mutes ( user_id, guild_id, moderator_id, reason, duration, start_time, end_time, - process_uuid, channel_id, mute_role_id, message_id, message_content, + process_uuid, channel_id, mute_role_id, message_id, message_content, message_attachments, message_author_id, message_channel_id, context_messages ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ - + cursor.execute(insert_query, ( user_id, guild_id, moderator_id, reason, duration, start_time, end_time, - str(process_uuid) if process_uuid else None, channel_id, mute_role_id, - message_id, message_content, message_attachments, message_author_id, + str(process_uuid) if process_uuid else None, channel_id, mute_role_id, + message_id, message_content, message_attachments, message_author_id, message_channel_id, context_messages )) - + mute_id = cursor.lastrowid connection.commit() - + logger.info(f"Mute record saved to database: ID={mute_id}, User={user_id}, Guild={guild_id}") return mute_id - + except Exception as e: logger.error(f"Error saving mute to database: {e}") if connection: