discordbot/gurt/emojis.py
Slipstream f0de735d13
feat: Improve custom emoji and sticker handling in AI responses
Refactor AI response processing to correctly handle custom emojis and stickers.
Custom emojis are now converted to Discord's `<:name:id>` format for proper display.
Custom stickers are identified and their IDs are extracted to be sent as separate attachments, removing them from the main content.
This ensures Gurt can properly utilize learned custom emojis and stickers in its responses.
2025-05-28 14:48:58 -06:00

94 lines
4.0 KiB
Python

import json
import os
from typing import Dict, Optional, Tuple, Union
DATA_FILE_PATH = "data/custom_emojis_stickers.json"
class EmojiManager:
def __init__(self, data_file: str = DATA_FILE_PATH):
self.data_file = data_file
self.data: Dict[str, Dict[str, str]] = {"emojis": {}, "stickers": {}}
self._load_data()
def _load_data(self):
"""Loads emoji and sticker data from the JSON file."""
try:
if os.path.exists(self.data_file):
with open(self.data_file, 'r', encoding='utf-8') as f:
loaded_json = json.load(f)
if isinstance(loaded_json, dict):
self.data["emojis"] = loaded_json.get("emojis", {})
self.data["stickers"] = loaded_json.get("stickers", {})
print(f"Loaded {len(self.data['emojis'])} emojis and {len(self.data['stickers'])} stickers from {self.data_file}")
else:
print(f"Warning: Data in {self.data_file} is not a dictionary. Initializing with empty data.")
self._save_data() # Initialize with empty structure if format is wrong
else:
print(f"{self.data_file} not found. Initializing with empty data.")
self._save_data() # Create the file if it doesn't exist
except json.JSONDecodeError:
print(f"Error decoding JSON from {self.data_file}. Initializing with empty data.")
self._save_data()
except Exception as e:
print(f"Error loading emoji/sticker data: {e}")
# Ensure data is initialized even on other errors
if "emojis" not in self.data: self.data["emojis"] = {}
if "stickers" not in self.data: self.data["stickers"] = {}
def _save_data(self):
"""Saves the current emoji and sticker data to the JSON file."""
try:
os.makedirs(os.path.dirname(self.data_file), exist_ok=True)
with open(self.data_file, 'w', encoding='utf-8') as f:
json.dump(self.data, f, indent=4)
print(f"Saved emoji and sticker data to {self.data_file}")
return True
except Exception as e:
print(f"Error saving emoji/sticker data: {e}")
return False
async def add_emoji(self, name: str, emoji_id: str, is_animated: bool) -> bool:
"""Adds a custom emoji."""
if name in self.data["emojis"]:
return False # Emoji already exists
self.data["emojis"][name] = {"id": emoji_id, "animated": is_animated}
return self._save_data()
async def remove_emoji(self, name: str) -> bool:
"""Removes a custom emoji."""
if name not in self.data["emojis"]:
return False # Emoji not found
del self.data["emojis"][name]
return self._save_data()
async def list_emojis(self) -> Dict[str, Dict[str, Union[str, bool]]]:
"""Lists all custom emojis."""
return self.data["emojis"]
async def get_emoji(self, name: str) -> Optional[Dict[str, Union[str, bool]]]:
"""Gets a specific custom emoji by name."""
return self.data["emojis"].get(name)
async def add_sticker(self, name: str, sticker_id: str) -> bool:
"""Adds a custom sticker."""
if name in self.data["stickers"]:
return False # Sticker already exists
self.data["stickers"][name] = {"id": sticker_id}
return self._save_data()
async def remove_sticker(self, name: str) -> bool:
"""Removes a custom sticker."""
if name not in self.data["stickers"]:
return False # Sticker not found
del self.data["stickers"][name]
return self._save_data()
async def list_stickers(self) -> Dict[str, Dict[str, str]]:
"""Lists all custom stickers."""
return self.data["stickers"]
async def get_sticker(self, name: str) -> Optional[Dict[str, str]]:
"""Gets a specific custom sticker by name."""
return self.data["stickers"].get(name)