feat: Enhance proactive AI response to support custom emoji and sticker handling with guild ID validation
This commit is contained in:
parent
2821f43454
commit
7b4a3e8abc
69
gurt/api.py
69
gurt/api.py
@ -1722,31 +1722,68 @@ async def get_proactive_ai_response(cog: 'GurtCog', message: discord.Message, tr
|
||||
if error_message and "error" not in final_parsed_data:
|
||||
final_parsed_data["error"] = error_message
|
||||
|
||||
sticker_ids_to_send_proactive: List[str] = [] # Initialize list for sticker IDs
|
||||
|
||||
# --- Handle Custom Emoji/Sticker Replacement in Proactive Content ---
|
||||
if final_parsed_data and final_parsed_data.get("content"):
|
||||
content_to_process = final_parsed_data["content"]
|
||||
potential_custom_items = re.findall(r':([\w\d_]+):', content_to_process)
|
||||
# Find all potential custom emoji/sticker names like :name:
|
||||
potential_custom_items = re.findall(r':([\w\d_]+?):', content_to_process)
|
||||
modified_content = content_to_process
|
||||
|
||||
for item_name_key in potential_custom_items:
|
||||
full_item_name = f":{item_name_key}:"
|
||||
emoji_url = await cog.emoji_manager.get_emoji(full_item_name)
|
||||
if emoji_url:
|
||||
print(f"Found known custom emoji '{full_item_name}' in proactive response content.")
|
||||
# No replacement, AI uses name.
|
||||
full_item_name_with_colons = f":{item_name_key}:"
|
||||
|
||||
# Check for custom emoji (logic remains similar to main response)
|
||||
emoji_data = await cog.emoji_manager.get_emoji(full_item_name_with_colons)
|
||||
can_use_emoji = False
|
||||
if isinstance(emoji_data, dict):
|
||||
emoji_id = emoji_data.get("id")
|
||||
is_animated = emoji_data.get("animated", False)
|
||||
emoji_guild_id = emoji_data.get("guild_id")
|
||||
|
||||
sticker_url = await cog.emoji_manager.get_sticker(full_item_name)
|
||||
if sticker_url:
|
||||
print(f"Found known custom sticker '{full_item_name}' in proactive response content.")
|
||||
if sticker_url not in modified_content:
|
||||
modified_content = f"{modified_content}\n{sticker_url}".strip()
|
||||
print(f"Appended sticker URL for '{full_item_name}' to proactive content: {sticker_url}")
|
||||
if emoji_id:
|
||||
if emoji_guild_id is not None:
|
||||
try:
|
||||
guild = cog.bot.get_guild(int(emoji_guild_id))
|
||||
if guild: can_use_emoji = True
|
||||
except ValueError: pass # Invalid guild_id
|
||||
else: can_use_emoji = True # Usable if no guild_id
|
||||
|
||||
if can_use_emoji and emoji_id:
|
||||
discord_emoji_syntax = f"<{'a' if is_animated else ''}:{item_name_key}:{emoji_id}>"
|
||||
modified_content = modified_content.replace(full_item_name_with_colons, discord_emoji_syntax, 1)
|
||||
print(f"Proactive: Replaced custom emoji '{full_item_name_with_colons}' with {discord_emoji_syntax}")
|
||||
|
||||
# Check for custom sticker
|
||||
sticker_data = await cog.emoji_manager.get_sticker(full_item_name_with_colons)
|
||||
can_use_sticker = False
|
||||
if isinstance(sticker_data, dict):
|
||||
sticker_id = sticker_data.get("id")
|
||||
sticker_guild_id = sticker_data.get("guild_id")
|
||||
|
||||
if sticker_id:
|
||||
if sticker_guild_id is not None:
|
||||
try:
|
||||
guild = cog.bot.get_guild(int(sticker_guild_id))
|
||||
if guild: can_use_sticker = True
|
||||
except ValueError: pass # Invalid guild_id
|
||||
else: can_use_sticker = True # Usable if no guild_id
|
||||
|
||||
if can_use_sticker and sticker_id:
|
||||
if full_item_name_with_colons in modified_content:
|
||||
modified_content = modified_content.replace(full_item_name_with_colons, "", 1).strip()
|
||||
if sticker_id not in sticker_ids_to_send_proactive:
|
||||
sticker_ids_to_send_proactive.append(sticker_id)
|
||||
print(f"Proactive: Found custom sticker '{full_item_name_with_colons}', removed from content, added ID '{sticker_id}'")
|
||||
|
||||
if modified_content != final_parsed_data["content"]:
|
||||
final_parsed_data["content"] = modified_content
|
||||
print("Proactive content modified with custom emoji/sticker information.")
|
||||
# Clean up any double spaces or leading/trailing whitespace after replacements
|
||||
modified_content = re.sub(r'\s{2,}', ' ', modified_content).strip()
|
||||
final_parsed_data["content"] = modified_content
|
||||
if sticker_ids_to_send_proactive or (content_to_process != modified_content):
|
||||
print("Proactive content modified for custom emoji/sticker information.")
|
||||
|
||||
return final_parsed_data, [] # Return empty list for stickers
|
||||
return final_parsed_data, sticker_ids_to_send_proactive
|
||||
|
||||
|
||||
# --- Internal AI Call for Specific Tasks ---
|
||||
|
Loading…
x
Reference in New Issue
Block a user