使用db管理类进行工具初始化
This commit is contained in:
@@ -13,8 +13,10 @@ from enum import Enum
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from db.connection import DBConnectionManager
|
||||
|
||||
# 连接到本地 Redis 服务
|
||||
r = redis.StrictRedis(host='192.168.2.40', port=6379, db=0, decode_responses=True)
|
||||
# r = redis.StrictRedis(host='192.168.2.40', port=6379, db=0, decode_responses=True)
|
||||
|
||||
|
||||
class PermissionStatus(Enum):
|
||||
@@ -59,6 +61,12 @@ class Feature(Enum):
|
||||
return self.description
|
||||
|
||||
|
||||
def get_redis_connection():
|
||||
# 初始化时加载本地缓存
|
||||
db_manager: DBConnectionManager = DBConnectionManager.get_instance()
|
||||
return db_manager.get_redis_connection()
|
||||
|
||||
|
||||
class GroupBotManager:
|
||||
"""群机器人管理,支持本地缓存"""
|
||||
|
||||
@@ -81,6 +89,7 @@ class GroupBotManager:
|
||||
|
||||
@staticmethod
|
||||
def load_local_cache():
|
||||
r = get_redis_connection()
|
||||
"""从 Redis 加载数据到本地缓存"""
|
||||
group_list = r.smembers("group:list")
|
||||
# 输出group_list 列表
|
||||
@@ -100,6 +109,7 @@ class GroupBotManager:
|
||||
|
||||
@staticmethod
|
||||
def save_to_redis():
|
||||
r = get_redis_connection()
|
||||
"""将本地缓存保存回 Redis"""
|
||||
# 保存 group:list 到 Redis
|
||||
r.sadd("group:list", *GroupBotManager.local_cache["group_list"])
|
||||
@@ -112,6 +122,7 @@ class GroupBotManager:
|
||||
|
||||
@staticmethod
|
||||
def set_group_permission(group_id, feature: Feature, status: PermissionStatus):
|
||||
r = get_redis_connection()
|
||||
"""设置群组功能权限并更新本地缓存"""
|
||||
# 更新本地缓存
|
||||
if group_id not in GroupBotManager.local_cache["group_permissions"]:
|
||||
@@ -185,6 +196,7 @@ class GroupBotManager:
|
||||
except KeyError:
|
||||
return "无效功能名称"
|
||||
|
||||
r = get_redis_connection()
|
||||
# 处理群机器人的启用和关闭(特别操作:更新 group:list)
|
||||
if feature == Feature.ROBOT:
|
||||
if action == "启用":
|
||||
@@ -283,7 +295,7 @@ class GroupBotManager:
|
||||
@staticmethod
|
||||
def remove_group(group_id):
|
||||
"""一键清除某个群的所有设置,用于退群或关闭群时处理
|
||||
|
||||
|
||||
Args:
|
||||
group_id: 群ID
|
||||
|
||||
@@ -294,6 +306,7 @@ class GroupBotManager:
|
||||
if group_id not in GroupBotManager.local_cache["group_list"]:
|
||||
return f"群 {group_id} 不在机器人管理列表中"
|
||||
|
||||
r = get_redis_connection()
|
||||
# 从本地缓存中移除群组
|
||||
GroupBotManager.local_cache["group_list"].remove(group_id)
|
||||
if group_id in GroupBotManager.local_cache["group_permissions"]:
|
||||
|
||||
Reference in New Issue
Block a user