1.新增私聊接口2.新增.talk指令

This commit is contained in:
2025-11-03 10:14:38 +08:00
parent ff709eadca
commit d88edc31fc
8 changed files with 841 additions and 14 deletions

View File

@@ -321,6 +321,9 @@ class Database:
self._add_column_if_not_exists('casino_bets', 'bet_value', "TEXT")
self._add_column_if_not_exists('casino_bets', 'hand_status', "TEXT")
# 兼容性检查为users表添加webhook_url字段
self._add_column_if_not_exists('users', 'webhook_url', 'TEXT')
logger.info("数据库表初始化完成")
# ===== 用户相关操作 =====
@@ -412,6 +415,103 @@ class Database:
logger.error(f"更新用户名失败: user_id={user_id}, username={username}, error={e}", exc_info=True)
return False
def set_user_webhook_url(self, user_id: int, webhook_url: str) -> bool:
"""设置用户webhook URL
Args:
user_id: 用户ID
webhook_url: Webhook URL
Returns:
是否成功
"""
try:
# 确保用户存在
self.get_or_create_user(user_id)
cursor = self.conn.cursor()
cursor.execute(
"UPDATE users SET webhook_url = ? WHERE user_id = ?",
(webhook_url, user_id)
)
logger.info(f"用户 {user_id} 设置webhook URL: {webhook_url}")
return True
except Exception as e:
logger.error(f"设置用户webhook URL失败: user_id={user_id}, error={e}", exc_info=True)
return False
def get_user_webhook_url(self, user_id: int) -> Optional[str]:
"""获取用户webhook URL
Args:
user_id: 用户ID
Returns:
Webhook URL如果不存在返回None
"""
cursor = self.conn.cursor()
cursor.execute(
"SELECT webhook_url FROM users WHERE user_id = ?",
(user_id,)
)
row = cursor.fetchone()
if not row:
return None
webhook_url = row[0]
if not webhook_url or webhook_url.strip() == '':
return None
return webhook_url
def has_webhook_url(self, user_id: int) -> bool:
"""检查用户是否有个人webhook URL
Args:
user_id: 用户ID
Returns:
是否有个人URL
"""
webhook_url = self.get_user_webhook_url(user_id)
return webhook_url is not None
def check_users_webhook_urls(self, user_ids: List[int]) -> Dict[int, bool]:
"""批量检查用户是否有个人webhook URL
Args:
user_ids: 用户ID列表
Returns:
字典 {user_id: has_url}
"""
if not user_ids:
return {}
# 初始化结果字典所有用户默认为False
results = {user_id: False for user_id in user_ids}
cursor = self.conn.cursor()
# 使用IN子句查询
placeholders = ','.join('?' * len(user_ids))
cursor.execute(
f"SELECT user_id, webhook_url FROM users WHERE user_id IN ({placeholders})",
user_ids
)
rows = cursor.fetchall()
for row in rows:
user_id = row[0]
webhook_url = row[1]
# 如果webhook_url不为None且不为空字符串则设为True
if webhook_url and webhook_url.strip() != '':
results[user_id] = True
return results
def get_user_display_name(self, user_id: int) -> str:
"""获取用户显示名称
如果用户已注册username不为None返回用户名否则返回"用户{user_id}"

View File

@@ -1,6 +1,6 @@
"""数据模型定义"""
from pydantic import BaseModel, Field
from typing import Optional, Dict, Any
from typing import Optional, Dict, Any, List
class CallbackRequest(BaseModel):
@@ -76,3 +76,20 @@ class QuizGameState(GameState):
attempts: int = Field(0, description="尝试次数")
max_attempts: int = Field(3, description="最大尝试次数")
class PrivateMessageRequest(BaseModel):
"""私聊消息请求模型"""
user_id: int = Field(..., description="目标用户ID")
content: str = Field(..., description="消息内容")
msg_type: str = Field(default="text", description="消息类型: text 或 markdown")
class CheckBatchRequest(BaseModel):
"""批量检查请求模型"""
user_ids: List[int] = Field(..., description="用户ID列表")
class CheckBatchResponse(BaseModel):
"""批量检查响应模型"""
results: Dict[int, bool] = Field(..., description="用户ID到是否有URL的映射")