from __future__ import annotations import logging from typing import Any from yuxi.channel.extensions.qqbot.types import GateDecision logger = logging.getLogger(__name__) class GroupGatingEngine: def __init__( self, config: dict, bot_openid: str | None = None, security: Any | None = None, ): self._config = config self._bot_openid = bot_openid self._security = security async def resolve_gate_decision(self, ctx: Any) -> GateDecision: msg = ctx.msg group_openid = msg.group_openid or "" is_at = "GROUP_AT_MESSAGE_CREATE" in (msg.event_type or "") has_any_mention = bool(msg.mentions) was_mentioned = self._bot_openid in (msg.mentions or []) group_cfg = self._get_group_config(group_openid) ignore_other_mentions = group_cfg.get("ignore_other_mentions", True) require_mention = group_cfg.get("require_mention", True) if ignore_other_mentions and has_any_mention and not was_mentioned: return GateDecision.DROP_OTHER_MENTION if msg.content.startswith("/"): is_control = self._is_control_command(msg.content) if is_control and not self._is_authorized(msg.sender_id): return GateDecision.BLOCK_UNAUTHORIZED_COMMAND if is_control: return GateDecision.PROCESS if require_mention and not is_at: return GateDecision.SKIP_NO_MENTION return GateDecision.PROCESS def _get_group_config(self, group_openid: str) -> dict: groups = self._config.get("channels", {}).get("qqbot", {}).get("groups", {}) return groups.get(group_openid, {}) def _is_control_command(self, content: str) -> bool: control_prefixes = ("/bot-approve", "/bot-streaming", "/bot-clear-storage", "/bot-upgrade") return content.startswith(control_prefixes) def _is_authorized(self, sender_id: str) -> bool: if self._security: return self._security.check_dm_access(sender_id) return False