fix: Improve sticker handling with detailed logging and guild ID validation
This commit is contained in:
parent
c2b10eae42
commit
682cceaa43
57
gurt/api.py
57
gurt/api.py
@ -1516,26 +1516,36 @@ async def get_ai_response(cog: 'GurtCog', message: discord.Message, model_name:
|
||||
# Check if it's a known custom sticker
|
||||
sticker_data = await cog.emoji_manager.get_sticker(full_item_name_with_colons)
|
||||
can_use_sticker = False
|
||||
print(f"[GET_AI_RESPONSE] Checking sticker: '{full_item_name_with_colons}'. Found data: {sticker_data}")
|
||||
if isinstance(sticker_data, dict):
|
||||
sticker_id = sticker_data.get("id")
|
||||
sticker_guild_id = sticker_data.get("guild_id")
|
||||
print(f"[GET_AI_RESPONSE] Sticker '{full_item_name_with_colons}': ID='{sticker_id}', GuildID='{sticker_guild_id}'")
|
||||
|
||||
if sticker_id:
|
||||
if sticker_guild_id is not None:
|
||||
try:
|
||||
guild = cog.bot.get_guild(int(sticker_guild_id))
|
||||
guild_id_int = int(sticker_guild_id)
|
||||
guild = cog.bot.get_guild(guild_id_int)
|
||||
if guild:
|
||||
can_use_sticker = True
|
||||
print(f"Sticker '{full_item_name_with_colons}' belongs to guild '{guild.name}' ({sticker_guild_id}), bot is a member.")
|
||||
print(f"[GET_AI_RESPONSE] Sticker '{full_item_name_with_colons}' (Guild: {guild.name} ({sticker_guild_id})) - Bot IS a member. CAN USE.")
|
||||
else:
|
||||
print(f"Cannot use sticker '{full_item_name_with_colons}'. Bot is not in guild ID: {sticker_guild_id}.")
|
||||
print(f"[GET_AI_RESPONSE] Sticker '{full_item_name_with_colons}' (Guild ID: {sticker_guild_id}) - Bot is NOT in this guild. CANNOT USE.")
|
||||
except ValueError:
|
||||
print(f"Invalid guild_id format for sticker '{full_item_name_with_colons}': {sticker_guild_id}")
|
||||
print(f"[GET_AI_RESPONSE] Invalid guild_id format for sticker '{full_item_name_with_colons}': {sticker_guild_id}. CANNOT USE.")
|
||||
else: # guild_id is None, considered usable
|
||||
can_use_sticker = True
|
||||
print(f"Sticker '{full_item_name_with_colons}' has no associated guild_id, allowing usage.")
|
||||
|
||||
if can_use_sticker and sticker_id:
|
||||
print(f"[GET_AI_RESPONSE] Sticker '{full_item_name_with_colons}' has no associated guild_id. CAN USE.")
|
||||
else:
|
||||
print(f"[GET_AI_RESPONSE] Sticker '{full_item_name_with_colons}' found in data, but no ID. CANNOT USE.")
|
||||
else:
|
||||
print(f"[GET_AI_RESPONSE] Sticker '{full_item_name_with_colons}' not found in emoji_manager or data is not dict.")
|
||||
|
||||
print(f"[GET_AI_RESPONSE] Final check for sticker '{full_item_name_with_colons}': can_use_sticker={can_use_sticker}, sticker_id='{sticker_data.get('id') if isinstance(sticker_data, dict) else None}'")
|
||||
if can_use_sticker and isinstance(sticker_data, dict) and sticker_data.get("id"):
|
||||
sticker_id_to_add = sticker_data.get("id") # Re-fetch to be safe
|
||||
if sticker_id_to_add: # Ensure ID is valid before proceeding
|
||||
# Remove the sticker text from the content (only the first instance)
|
||||
if full_item_name_with_colons in modified_content:
|
||||
modified_content = modified_content.replace(full_item_name_with_colons, "", 1).strip()
|
||||
@ -1787,24 +1797,41 @@ async def get_proactive_ai_response(cog: 'GurtCog', message: discord.Message, tr
|
||||
# Check for custom sticker
|
||||
sticker_data = await cog.emoji_manager.get_sticker(full_item_name_with_colons)
|
||||
can_use_sticker = False
|
||||
print(f"[PROACTIVE] Checking sticker: '{full_item_name_with_colons}'. Found data: {sticker_data}")
|
||||
if isinstance(sticker_data, dict):
|
||||
sticker_id = sticker_data.get("id")
|
||||
sticker_guild_id = sticker_data.get("guild_id")
|
||||
print(f"[PROACTIVE] Sticker '{full_item_name_with_colons}': ID='{sticker_id}', GuildID='{sticker_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
|
||||
guild_id_int = int(sticker_guild_id)
|
||||
guild = cog.bot.get_guild(guild_id_int)
|
||||
if guild:
|
||||
can_use_sticker = True
|
||||
print(f"[PROACTIVE] Sticker '{full_item_name_with_colons}' (Guild: {guild.name} ({sticker_guild_id})) - Bot IS a member. CAN USE.")
|
||||
else:
|
||||
print(f"[PROACTIVE] Sticker '{full_item_name_with_colons}' (Guild ID: {sticker_guild_id}) - Bot is NOT in this guild. CANNOT USE.")
|
||||
except ValueError:
|
||||
print(f"[PROACTIVE] Invalid guild_id format for sticker '{full_item_name_with_colons}': {sticker_guild_id}. CANNOT USE.")
|
||||
else: # guild_id is None, considered usable
|
||||
can_use_sticker = True
|
||||
print(f"[PROACTIVE] Sticker '{full_item_name_with_colons}' has no associated guild_id. CAN USE.")
|
||||
else:
|
||||
print(f"[PROACTIVE] Sticker '{full_item_name_with_colons}' found in data, but no ID. CANNOT USE.")
|
||||
else:
|
||||
print(f"[PROACTIVE] Sticker '{full_item_name_with_colons}' not found in emoji_manager or data is not dict.")
|
||||
|
||||
if can_use_sticker and sticker_id:
|
||||
print(f"[PROACTIVE] Final check for sticker '{full_item_name_with_colons}': can_use_sticker={can_use_sticker}, sticker_id='{sticker_data.get('id') if isinstance(sticker_data, dict) else None}'")
|
||||
if can_use_sticker and isinstance(sticker_data, dict) and sticker_data.get("id"):
|
||||
sticker_id_to_add = sticker_data.get("id") # Re-fetch to be safe
|
||||
if sticker_id_to_add: # Ensure ID is valid
|
||||
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 sticker_id_to_add not in sticker_ids_to_send_proactive:
|
||||
sticker_ids_to_send_proactive.append(sticker_id_to_add)
|
||||
print(f"Proactive: Found custom sticker '{full_item_name_with_colons}', removed from content, added ID '{sticker_id_to_add}'")
|
||||
|
||||
# Clean up any double spaces or leading/trailing whitespace after replacements
|
||||
modified_content = re.sub(r'\s{2,}', ' ', modified_content).strip()
|
||||
|
Loading…
x
Reference in New Issue
Block a user