ForcePilot/backend/package/yuxi/channels/adapters/twitch/resolver.py
Kris 59cd13cf84 feat(twitch): 实现完整的Twitch聊天适配器模块
新增Twitch IRC协议相关的全套实现,包括:
1. 基础工具类:令牌处理、消息格式化、速率限制、消息去重
2. 核心适配器组件:IRC解析器、消息归一化、外发消息处理
3. API客户端:Helix API封装、认证提供者
4. 配置与部署:配置校验、设置向导
5. 辅助功能:配对管理、健康检查、目标解析等
2026-05-12 00:50:10 +08:00

48 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import Any
from yuxi.utils.logging_config import logger
from .helix import HelixClient
def _is_numeric_id(value: str) -> bool:
return value.isdigit()
async def resolve_twitch_target(
input_value: str,
client_id: str,
access_token: str,
) -> dict[str, Any] | None:
"""将输入(用户名或用户 ID解析为 Twitch 用户信息"""
value = input_value.strip().lstrip("@").lower()
if not value:
return None
if not client_id or not access_token:
logger.warning("resolve_twitch_target: missing credentials")
return None
helix = HelixClient(client_id, access_token)
try:
await helix.start()
if _is_numeric_id(value):
user = await helix.get_user_by_id(value)
if user:
return user
return None
user = await helix.get_user_by_name(value)
if user:
return user
return None
except Exception as e:
logger.error(f"resolve_twitch_target error for '{input_value}': {e}")
return None
finally:
await helix.close()