fix: Enhance author identification logic to utilize cached author strings for improved efficiency

This commit is contained in:
Slipstream 2025-05-30 17:52:30 -06:00
parent c7e15f5581
commit 28960c4c0c
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -920,36 +920,45 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
full_text = "\n".join(text_parts).strip()
if full_text: # Only add if there's some text content
author_details = msg.get("author", {})
raw_display_name = author_details.get("display_name")
raw_name = author_details.get("name") # Discord username
author_id = author_details.get("id")
author_string_from_cache = msg.get("author_string")
final_display_part = ""
username_part_str = ""
if author_string_from_cache and str(author_string_from_cache).strip():
# If author_string is available and valid from the cache, use it directly.
# This string is expected to be pre-formatted by the context gathering logic.
author_identifier_string = str(author_string_from_cache)
parts.append(types.Part(text=f"{author_identifier_string}: {full_text}"))
else:
# Fallback to reconstructing the author identifier if author_string is not available/valid
author_details = msg.get("author", {})
raw_display_name = author_details.get("display_name")
raw_name = author_details.get("name") # Discord username
author_id = author_details.get("id")
if raw_display_name and str(raw_display_name).strip():
final_display_part = str(raw_display_name)
elif raw_name and str(raw_name).strip(): # Fallback display to username
final_display_part = str(raw_name)
elif author_id: # Fallback display to User ID
final_display_part = f"User ID: {author_id}"
else: # Default to "Unknown User" if no other identifier is found
final_display_part = "Unknown User"
final_display_part = ""
username_part_str = ""
# Construct username part if raw_name is valid and different from final_display_part
if raw_name and str(raw_name).strip() and str(raw_name).lower() != "none":
# Avoid "Username (Username: Username)" if display name fell back to raw_name
if final_display_part.lower() != str(raw_name).lower():
username_part_str = f" (Username: {str(raw_name)})"
# If username is bad/missing, but we have an ID, and ID isn't already the main display part
elif author_id and not (raw_name and str(raw_name).strip() and str(raw_name).lower() != "none"):
if not final_display_part.startswith("User ID:"):
username_part_str = f" (User ID: {author_id})"
if raw_display_name and str(raw_display_name).strip():
final_display_part = str(raw_display_name)
elif raw_name and str(raw_name).strip(): # Fallback display to username
final_display_part = str(raw_name)
elif author_id: # Fallback display to User ID
final_display_part = f"User ID: {author_id}"
else: # Default to "Unknown User" if no other identifier is found
final_display_part = "Unknown User"
author_identifier_string = f"{final_display_part}{username_part_str}"
# Append the text part to the existing parts list for this message
parts.append(types.Part(text=f"{author_identifier_string}: {full_text}"))
# Construct username part if raw_name is valid and different from final_display_part
if raw_name and str(raw_name).strip() and str(raw_name).lower() != "none":
# Avoid "Username (Username: Username)" if display name fell back to raw_name
if final_display_part.lower() != str(raw_name).lower():
username_part_str = f" (Username: {str(raw_name)})"
# If username is bad/missing, but we have an ID, and ID isn't already the main display part
elif author_id and not (raw_name and str(raw_name).strip() and str(raw_name).lower() != "none"):
if not final_display_part.startswith("User ID:"):
username_part_str = f" (User ID: {author_id})"
author_identifier_string = f"{final_display_part}{username_part_str}"
# Append the text part to the existing parts list for this message
parts.append(types.Part(text=f"{author_identifier_string}: {full_text}"))
# Only append to contents if there are parts to add for this message
if parts: