import logging from yuxi.channel.extensions.minecraft.config import McAccountConfig, McConfig from yuxi.channel.extensions.minecraft.types import ResolvedMcAccount logger = logging.getLogger(__name__) def list_mc_account_ids(cfg: McConfig) -> list[str]: if cfg.accounts: return [acc.account_id for acc in cfg.accounts] return ["default"] def resolve_mc_account(cfg: McConfig, account_id: str = "default") -> ResolvedMcAccount: account_cfg = cfg.get_account(account_id) if account_cfg is None: if account_id == "default": account_cfg = McAccountConfig( host=cfg.host, port=cfg.port, version=cfg.version, auth_mode=cfg.auth_mode, username=cfg.username, dm_policy=cfg.dm_policy, group_policy=cfg.group_policy, require_mention=cfg.require_mention, allow_from=cfg.allow_from, text_chunk_limit=cfg.text_chunk_limit, reconnect_max_retries=cfg.reconnect_max_retries, keep_alive_timeout=cfg.keep_alive_timeout, rate_limit_window=cfg.rate_limit_window, rate_limit_max=cfg.rate_limit_max, ) else: raise KeyError(f"Account '{account_id}' not found") host = account_cfg.host or cfg.host port = account_cfg.port or cfg.port version = account_cfg.version if account_cfg.host else cfg.version auth_mode = account_cfg.auth_mode if account_cfg.host else cfg.auth_mode username = account_cfg.username or cfg.username dm_policy = account_cfg.dm_policy if account_cfg.host else cfg.dm_policy group_policy = account_cfg.group_policy if account_cfg.host else cfg.group_policy require_mention = account_cfg.require_mention if account_cfg.host else cfg.require_mention allow_from = account_cfg.allow_from or cfg.allow_from text_chunk_limit = account_cfg.text_chunk_limit or cfg.text_chunk_limit reconnect_max_retries = account_cfg.reconnect_max_retries or cfg.reconnect_max_retries keep_alive_timeout = account_cfg.keep_alive_timeout or cfg.keep_alive_timeout rate_limit_window = account_cfg.rate_limit_window or cfg.rate_limit_window rate_limit_max = account_cfg.rate_limit_max or cfg.rate_limit_max return ResolvedMcAccount( account_id=account_id, enabled=account_cfg.enabled, name=account_cfg.name or account_id, host=host, port=port, version=version, auth_mode=auth_mode, username=username, microsoft_email=account_cfg.microsoft_email, microsoft_password=account_cfg.microsoft_password, rcon_port=account_cfg.rcon_port or cfg.rcon_port, rcon_password=account_cfg.rcon_password, dm_enabled=account_cfg.dm_enabled, dm_policy=dm_policy, group_policy=group_policy, require_mention=require_mention, allow_from=allow_from, text_chunk_limit=text_chunk_limit, reconnect_max_retries=reconnect_max_retries, keep_alive_timeout=keep_alive_timeout, rate_limit_window=rate_limit_window, rate_limit_max=rate_limit_max, ) def has_configured_state(cfg: McConfig) -> bool: if cfg.host and cfg.username: return True for acc in cfg.accounts: if acc.host and acc.username: return True return False