ForcePilot/backend/package/yuxi/channel/extensions/twitch/access_control.py
Kris 0babb1ca8e feat(channel): 添加 Tlon 渠道扩展
新增 Tlon 渠道扩展,支持在 Yuxi 平台中集成 Tlon/Urbit 去中心化通讯平台。

包含以下功能模块:
- tlon_api: Tlon API 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- sse_client: SSE 客户端
- outbound: 外发消息管理
- send: 消息发送
- security: 安全校验
- auth: 认证管理
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- approval: 审批流程
- channel_mgmt: 频道管理
- channel_ops: 频道操作
- contacts: 联系人管理
- discovery: 服务发现
- doctor: 健康诊断
- expose: 服务暴露
- gallery: 图库管理
- history: 历史记录
- hooks: 钩子管理
- media: 媒体资源处理
- notebook: 笔记本功能
- settings_store: 设置存储
- setup: 初始化设置
- story: 故事功能
- targets: 目标管理
- cite_parser: 引用解析
- utils: 工具函数
- types: 类型定义
2026-05-21 11:55:25 +08:00

63 lines
2.4 KiB
Python

import re
from yuxi.channel.extensions.twitch.config import TwitchAccountConfig
from yuxi.channel.extensions.twitch.types import TwitchAccessControlResult, TwitchChatMessage, TwitchRole
def check_twitch_access_control(
message: TwitchChatMessage,
account: TwitchAccountConfig,
bot_username: str,
) -> TwitchAccessControlResult:
if account.require_mention and not _mentions_bot(message.message, bot_username):
return TwitchAccessControlResult(
allowed=False,
reason="message does not mention the bot",
)
if account.allow_from:
if not message.user_id:
return TwitchAccessControlResult(
allowed=False,
reason="sender user ID not available",
)
if message.user_id not in account.allow_from:
return TwitchAccessControlResult(
allowed=False,
reason=f"user {message.user_id} not in allowlist",
)
return TwitchAccessControlResult(
allowed=True,
match_key=message.user_id,
match_source="allowlist",
)
if account.allowed_roles:
for role in account.allowed_roles:
if role == TwitchRole.ALL:
return TwitchAccessControlResult(
allowed=True,
match_key="all",
match_source="role",
)
if role == TwitchRole.MODERATOR and message.is_mod:
return TwitchAccessControlResult(allowed=True, match_key="moderator", match_source="role")
if role == TwitchRole.OWNER and message.is_owner:
return TwitchAccessControlResult(allowed=True, match_key="owner", match_source="role")
if role == TwitchRole.VIP and message.is_vip:
return TwitchAccessControlResult(allowed=True, match_key="vip", match_source="role")
if role == TwitchRole.SUBSCRIBER and message.is_sub:
return TwitchAccessControlResult(allowed=True, match_key="subscriber", match_source="role")
return TwitchAccessControlResult(
allowed=False,
reason="sender does not have required roles",
)
return TwitchAccessControlResult(allowed=True)
def _mentions_bot(message_text: str, bot_username: str) -> bool:
mentions = re.findall(r"@(\w+)", message_text)
bot_lower = bot_username.lower()
return any(m.lower() == bot_lower for m in mentions)