| | import logging |
| | from db_utils import get_db_connection, release_db_connection |
| | from bot_utils import send_bot_message |
| |
|
| | def get_user_config(user_id, chat_id): |
| | """获取用户的配置信息。""" |
| | db_connection = get_db_connection() |
| | if not db_connection: |
| | return {} |
| |
|
| | try: |
| | with db_connection.cursor() as cursor: |
| | query = "SELECT user_id, chat_id, ai_api, ai_model, ai_key, ai_prompt, max_token, message_delete_delay, created_at FROM user_configs WHERE " |
| | params = [] |
| | if chat_id < 0: |
| | query += "chat_id = %s" |
| | params.append(chat_id) |
| | else: |
| | query += "user_id = %s AND chat_id = %s" |
| | params.extend([user_id, chat_id]) |
| |
|
| | cursor.execute(query, params) |
| | row = cursor.fetchone() |
| |
|
| | if row: |
| | return dict(zip(["user_id", "chat_id", "ai_api", "ai_model", "ai_key", "ai_prompt", "max_token", "message_delete_delay", "created_at"], row)) |
| | else: |
| | return {} |
| | except Exception as e: |
| | logging.error(f"获取用户 {user_id} 在聊天 {chat_id} 中的配置失败: {e}") |
| | return {} |
| | finally: |
| | release_db_connection(db_connection) |
| |
|
| | def set_user_config(user_id, config, chat_id, message=None, bot=None): |
| | db_connection = get_db_connection() |
| | if not db_connection: |
| | logging.error("无法获取数据库连接,配置设置失败") |
| | return False |
| |
|
| | try: |
| | with db_connection.cursor() as cursor: |
| | if chat_id < 0 and not is_group_admin(chat_id, user_id, message, bot): |
| | if bot: |
| | send_bot_message(bot, chat_id, "群组配置仅限于管理员", parse_mode="MarkdownV2") |
| | else: |
| | logging.error("在 set_user_config 中尝试发送消息,但 bot 对象未初始化") |
| | return False |
| |
|
| | user_id = 0 if chat_id < 0 else user_id |
| |
|
| | cursor.execute(""" |
| | INSERT INTO user_configs (user_id, chat_id, ai_api, ai_model, ai_key, ai_prompt, max_token, message_delete_delay) |
| | VALUES (%s, %s, %s, %s, %s, %s, %s, %s) |
| | ON CONFLICT (user_id, chat_id) DO UPDATE SET |
| | ai_api = EXCLUDED.ai_api, |
| | ai_model = EXCLUDED.ai_model, |
| | ai_key = EXCLUDED.ai_key, |
| | ai_prompt = EXCLUDED.ai_prompt, |
| | max_token = EXCLUDED.max_token, |
| | message_delete_delay = EXCLUDED.message_delete_delay |
| | """, (user_id, chat_id, config.get("ai_api"), config.get("ai_model"), config.get("ai_key"), config.get("ai_prompt"), config.get("max_token"), config.get("message_delete_delay"))) |
| | db_connection.commit() |
| | logging.info(f"用户 {user_id} 在聊天 {chat_id} 中的配置已更新") |
| | return True |
| | except Exception as e: |
| | logging.error(f"设置用户 {user_id} 在聊天 {chat_id} 中的配置失败: {e}") |
| | return False |
| | finally: |
| | release_db_connection(db_connection) |
| |
|
| | def is_config_complete(config, chat_id, message): |
| | required_keys = ["ai_api", "ai_model", "ai_key"] |
| | return all(config.get(key) for key in required_keys) |
| |
|
| | def is_group_admin(chat_id, user_id, message, bot): |
| | try: |
| | if not bot: |
| | logging.error("在 is_group_admin 中尝试获取管理员状态,但 bot 对象未初始化") |
| | return False |
| |
|
| | if message and message.sender_chat and message.sender_chat.id == chat_id: |
| | return True |
| |
|
| | member_status = bot.get_chat_member(chat_id, user_id).status |
| | return member_status in ["administrator", "creator"] |
| | except Exception as e: |
| | logging.error(f"验证用户 {user_id} 在聊天 {chat_id} 中的管理员状态失败: {e}") |
| | return False |
| |
|
| | def update_config_value(user_id, chat_id, config_map, command, new_value, message=None, bot=None): |
| | config = get_user_config(user_id, chat_id) |
| |
|
| | if command == "prompt" and new_value is None: |
| | config["ai_prompt"] = None |
| | elif command == "max_token" and new_value is None: |
| | config["max_token"] = None |
| | elif command == "message_delete_delay" and new_value is None: |
| | config["message_delete_delay"] = None |
| | else: |
| | config[config_map[command]["key"]] = new_value |
| |
|
| | logging.info(f"更新配置: {config}") |
| |
|
| | if set_user_config(user_id, config, chat_id, message, bot): |
| | if command == "prompt" and new_value is None: |
| | send_bot_message(bot, chat_id, "系统提示已清空", parse_mode="MarkdownV2") |
| | elif command == "max_token" and new_value is None: |
| | send_bot_message(bot, chat_id, "最大 token 限制已清空", parse_mode="MarkdownV2") |
| | elif command == "message_delete_delay" and new_value is None: |
| | send_bot_message(bot, chat_id, "消息删除延迟已清空", parse_mode="MarkdownV2") |
| | else: |
| | send_bot_message(bot, chat_id, f"配置项 `{command}` 已设定", parse_mode="MarkdownV2") |