1.新增私聊接口2.新增.talk指令
This commit is contained in:
100
core/database.py
100
core/database.py
@@ -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}"
|
||||
|
||||
Reference in New Issue
Block a user