modified: bot.py
This commit is contained in:
78
bot.py
78
bot.py
@@ -4429,43 +4429,97 @@ def create_mutes_table():
|
|||||||
|
|
||||||
async def save_mute_to_database(user_id, guild_id, moderator_id, reason, duration, start_time, end_time,
|
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):
|
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"""
|
"""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
|
connection = None
|
||||||
cursor = None
|
cursor = None
|
||||||
try:
|
try:
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
# Extract message data if provided
|
|
||||||
message_content = None
|
message_content = None
|
||||||
message_attachments = None
|
message_attachments = None
|
||||||
message_author_id = None
|
message_author_id = None
|
||||||
message_channel_id = None
|
message_channel_id = None
|
||||||
context_messages = 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 message_data:
|
||||||
if isinstance(message_data, dict) and "main_message" in message_data:
|
if isinstance(message_data, dict) and "main_message" in message_data:
|
||||||
# New format with context
|
# New format with context
|
||||||
main_msg = message_data.get("main_message", {})
|
main_msg = message_data.get("main_message", {})
|
||||||
message_content = main_msg.get("content")
|
message_content = main_msg.get("content")
|
||||||
message_attachments = main_msg.get("attachments")
|
|
||||||
message_author_id = main_msg.get("author_id")
|
message_author_id = main_msg.get("author_id")
|
||||||
message_channel_id = main_msg.get("channel_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:
|
else:
|
||||||
# Old format or simple dict
|
# Old format or simple dict
|
||||||
message_content = message_data.get("content")
|
message_content = message_data.get("content")
|
||||||
message_attachments = message_data.get("attachments")
|
|
||||||
message_author_id = message_data.get("author_id")
|
message_author_id = message_data.get("author_id")
|
||||||
message_channel_id = message_data.get("channel_id")
|
message_channel_id = message_data.get("channel_id")
|
||||||
|
|
||||||
# Convert JSON fields
|
# Use local paths only
|
||||||
if message_attachments and isinstance(message_attachments, str):
|
message_attachments = extract_local_attachments(message_data.get("attachments"))
|
||||||
# Already JSON string
|
|
||||||
pass
|
|
||||||
elif message_attachments:
|
|
||||||
# Convert to JSON string
|
|
||||||
message_attachments = json.dumps(message_attachments)
|
|
||||||
|
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO user_mutes (
|
INSERT INTO user_mutes (
|
||||||
|
|||||||
Reference in New Issue
Block a user