"""List all servers/guilds and channels the bot is active in.
╔═══════════════════════════════════════════════════════════════════════════════╗
║ 🌐 CROSS-PLATFORM SERVER LISTING ║
╠═══════════════════════════════════════════════════════════════════════════════╣
║ list_active_servers — Shows every server, guild, and channel across all ║
║ connected platforms (Discord, Matrix, …) ║
╚═══════════════════════════════════════════════════════════════════════════════╝
"""
from __future__ import annotations
import json
import logging
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from tool_context import ToolContext
logger = logging.getLogger(__name__)
TOOL_NAME = "list_active_servers"
TOOL_DESCRIPTION = (
"List ALL servers, guilds, and channels the bot is currently active "
"in across every connected platform (Discord, Matrix, etc.). "
"Returns a comprehensive inventory grouped by platform, with each "
"server's channels listed under it."
)
TOOL_PARAMETERS = {
"type": "object",
"properties": {},
}
[docs]
async def run(ctx: "ToolContext | None" = None) -> str:
"""Return a JSON inventory of all platforms, servers, and channels."""
if ctx is None:
return json.dumps({"success": False, "error": "No tool context."})
adapters = getattr(ctx, "all_adapters", None) or []
if not adapters:
return json.dumps({
"success": False,
"error": "No platform adapters available.",
})
result: dict[str, Any] = {
"success": True,
"platform_count": 0,
"platforms": [],
}
for adapter in adapters:
platform_name = getattr(adapter, "name", "unknown")
is_running = getattr(adapter, "is_running", False)
platform_entry: dict[str, Any] = {
"platform": platform_name,
"running": is_running,
"servers": [],
}
if is_running:
try:
servers = await adapter.list_servers_and_channels()
platform_entry["servers"] = servers
platform_entry["server_count"] = len(servers)
platform_entry["total_channels"] = sum(
len(s.get("channels", [])) for s in servers
)
except Exception as exc:
logger.exception(
"Failed to list servers for platform %s", platform_name,
)
platform_entry["error"] = str(exc)
result["platforms"].append(platform_entry)
result["platform_count"] = len(result["platforms"])
return json.dumps(result, indent=2)