Files
docker-configs/discord_bot/utils/token_reader.py
Will Song 568dcc45e4 Fix router failover and add Discord bot
- Remove duplicate claude_api provider to fix automatic failover
- Enhance error detection with HTTP status codes and more indicators
- Add comprehensive README documentation with manual switching
- Implement Discord bot with Claude Code CLI integration
- Support /terminal and /claude commands with AI-powered responses

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-16 21:35:52 -05:00

48 lines
1.5 KiB
Python

import logging
from typing import Optional, Dict
logger = logging.getLogger(__name__)
class TokenReader:
"""Token文件读取器"""
def __init__(self, token_path: str):
self.token_path = token_path
self._tokens: Optional[Dict[str, str]] = None
def _load_tokens(self) -> Dict[str, str]:
"""加载token文件"""
if self._tokens is not None:
return self._tokens
tokens = {}
try:
with open(self.token_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
tokens[key.strip()] = value.strip()
self._tokens = tokens
logger.info(f"成功加载 {len(tokens)} 个token")
except Exception as e:
logger.error(f"读取token文件失败: {e}")
raise
return tokens
def get_token(self, key: str) -> Optional[str]:
"""获取指定的token"""
tokens = self._load_tokens()
return tokens.get(key)
def get_all_tokens(self) -> Dict[str, str]:
"""获取所有tokens"""
return self._load_tokens().copy()
def reload(self):
"""重新加载token文件"""
self._tokens = None
logger.info("Token文件已重新加载")