Fetch Discord user details and avatars for context-aware responses
The BioTools class provides agents with the ability to fetch detailed information about Discord users, including profiles, avatars, roles, and activity status.
from tools.bio_tools import BioToolsimport discord# With Discord client (recommended)bio_tools = BioTools(client=discord_client)# Without client (will attempt to fetch from context)bio_tools = BioTools()
BioTools uses a priority system to obtain the Discord client:
def _get_discord_client(self, channel=None) -> Optional[discord.Client]: # Priority 1: Use injected client if self.client: return self.client # Priority 2: Get from channel state (works for DMs) if channel and hasattr(channel, '_state'): client = channel._state._get_client() if client: return client # Priority 3: Get from guild (guild channels only) if channel: guild = getattr(channel, 'guild', None) if guild and hasattr(guild, '_state'): client = guild._state._get_client() if client: return client return None
BioTools attempts multiple strategies to fetch user data:
async def _fetch_user(self, user_id: int, channel=None): # For guild channels: Try to get Member (includes roles, status) if guild and not is_dm: member = guild.get_member(user_id) # Cache first if not member: member = await guild.fetch_member(user_id) # API fallback if member: return member # Fallback: Fetch User globally (works for both contexts) user = await client.fetch_user(user_id) return user
for activity in user.activities: if isinstance(activity, discord.CustomActivity): # Custom Status: Working on cool stuff 🚀 elif isinstance(activity, discord.Spotify): # Listening to Spotify: Song Name by Artist elif isinstance(activity, discord.Game): # Playing: Minecraft elif isinstance(activity, discord.Streaming): # Streaming: Live Coding (https://twitch.tv/...)
from tools.history_tools import HistoryToolsfrom tools.bio_tools import BioToolscontext_qna_agent = Agent( id="context-qna-agent", name="Chat Context Q&A", role="Answering questions about users, topics, and past conversations", model=OpenAILike( id=CONTEXT_AGENT_MODEL, max_tokens=8000, temperature=0.3, base_url=PROVIDER, api_key=CUSTOM_PROVIDER_API_KEY, ), tools=[HistoryTools(), BioTools(client=client)], add_datetime_to_context=True, timezone_identifier="Asia/Kolkata", instructions=""" You specialize in answering questions about chat history, users, and topics. Use `get_user_details` to fetch user information. Use `read_chat_history` to get conversation context. Be precise with timestamps and attribute statements accurately. """)
from tools.bio_tools import BioToolsbio_tools = BioTools(client=discord_client)# Get detailed user informationdetails = await bio_tools.get_user_details(user_id=123456789)print(details)
from tools.bio_tools import BioToolsfrom tools.history_tools import HistoryTools# Create agent with both toolsagent = Agent( name="User Context Agent", model=my_model, tools=[BioTools(client=client), HistoryTools()], instructions=""" Use get_user_details to learn about users. Use read_chat_history to understand conversation context. Combine both to provide comprehensive answers. """)
import logginglogger = logging.getLogger(__name__)# Log levels used:logger.debug(f"Could not get client from channel state: {e}")logger.warning(f"Could not fetch full user profile: {e}")logger.error(f"Error getting user details: {e}", exc_info=True)