"""Fetch a Discord user's profile information including avatar."""
from __future__ import annotations
import json
import logging
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from tool_context import ToolContext
logger = logging.getLogger(__name__)
TOOL_NAME = "get_user_profile"
TOOL_DESCRIPTION = (
"Fetch a Discord user's profile information (display name and avatar URL) by their user ID."
)
TOOL_PARAMETERS = {
"type": "object",
"properties": {
"target_user_id": {
"type": "string",
"description": "The Discord user ID to fetch profile for.",
},
},
"required": ["target_user_id"],
}
[docs]
async def run(target_user_id: str, ctx: ToolContext | None = None) -> str:
"""Execute this tool and return the result.
Args:
target_user_id (str): The target user id value.
ctx (ToolContext | None): Tool execution context providing access to bot internals.
Returns:
str: Result string.
"""
from tools._discord_helpers import require_discord_client
client = require_discord_client(ctx)
if isinstance(client, str):
return client
if not target_user_id:
return json.dumps({"error": "Missing required argument: target_user_id is required."})
try:
user = await client.fetch_user(int(target_user_id))
if not user:
return json.dumps({"error": f"User with ID {target_user_id} not found."})
user_info = {
"display_name": user.display_name,
"avatar_url": str(user.avatar.url) if user.avatar else None,
}
return json.dumps(user_info)
except Exception as e:
logger.error("Error fetching user profile for ID %s: %s", target_user_id, e, exc_info=True)
return json.dumps({"error": f"Error fetching user profile: {e}"})