"""Retrieve total guild count and member/channel roster for a specific guild."""
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_server_diagnostics"
TOOL_DESCRIPTION = (
"Get the total number of guilds the bot is in and the member/channel roster "
"for a specific guild by ID."
)
TOOL_PARAMETERS = {
"type": "object",
"properties": {
"guild_id": {
"type": "string",
"description": "The Discord guild (server) ID to get diagnostics for.",
},
},
"required": ["guild_id"],
}
[docs]
async def run(guild_id: str, ctx: ToolContext | None = None) -> str:
"""Execute this tool and return the result.
Args:
guild_id (str): Discord guild (server) identifier.
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
total_guilds = len(client.guilds)
try:
target_guild = client.get_guild(int(guild_id))
if not target_guild:
roster = "Guild not found"
else:
roster = {
"server_id": target_guild.id,
"server_name": target_guild.name,
"member_ids": [m.id for m in target_guild.members],
"channel_ids": [c.id for c in target_guild.channels],
}
except Exception as e:
roster = f"Error retrieving roster: {e}"
return json.dumps({
"total_guild_count": total_guilds,
"target_guild_roster": roster,
}, default=str)