fix: Enhance author identification logic to utilize cached author strings for improved efficiency
This commit is contained in:
parent
c7e15f5581
commit
28960c4c0c
63
gurt/api.py
63
gurt/api.py
@ -920,36 +920,45 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
|
|||||||
|
|
||||||
full_text = "\n".join(text_parts).strip()
|
full_text = "\n".join(text_parts).strip()
|
||||||
if full_text: # Only add if there's some text content
|
if full_text: # Only add if there's some text content
|
||||||
author_details = msg.get("author", {})
|
author_string_from_cache = msg.get("author_string")
|
||||||
raw_display_name = author_details.get("display_name")
|
|
||||||
raw_name = author_details.get("name") # Discord username
|
|
||||||
author_id = author_details.get("id")
|
|
||||||
|
|
||||||
final_display_part = ""
|
if author_string_from_cache and str(author_string_from_cache).strip():
|
||||||
username_part_str = ""
|
# 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 = ""
|
||||||
final_display_part = str(raw_display_name)
|
username_part_str = ""
|
||||||
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"
|
|
||||||
|
|
||||||
# Construct username part if raw_name is valid and different from final_display_part
|
if raw_display_name and str(raw_display_name).strip():
|
||||||
if raw_name and str(raw_name).strip() and str(raw_name).lower() != "none":
|
final_display_part = str(raw_display_name)
|
||||||
# Avoid "Username (Username: Username)" if display name fell back to raw_name
|
elif raw_name and str(raw_name).strip(): # Fallback display to username
|
||||||
if final_display_part.lower() != str(raw_name).lower():
|
final_display_part = str(raw_name)
|
||||||
username_part_str = f" (Username: {str(raw_name)})"
|
elif author_id: # Fallback display to User ID
|
||||||
# If username is bad/missing, but we have an ID, and ID isn't already the main display part
|
final_display_part = f"User ID: {author_id}"
|
||||||
elif author_id and not (raw_name and str(raw_name).strip() and str(raw_name).lower() != "none"):
|
else: # Default to "Unknown User" if no other identifier is found
|
||||||
if not final_display_part.startswith("User ID:"):
|
final_display_part = "Unknown User"
|
||||||
username_part_str = f" (User ID: {author_id})"
|
|
||||||
|
# Construct username part if raw_name is valid and different from final_display_part
|
||||||
author_identifier_string = f"{final_display_part}{username_part_str}"
|
if raw_name and str(raw_name).strip() and str(raw_name).lower() != "none":
|
||||||
# Append the text part to the existing parts list for this message
|
# Avoid "Username (Username: Username)" if display name fell back to raw_name
|
||||||
parts.append(types.Part(text=f"{author_identifier_string}: {full_text}"))
|
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
|
# Only append to contents if there are parts to add for this message
|
||||||
if parts:
|
if parts:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user