1
0
forked from bot/app
LiteyukiBot/liteyuki/utils/base/data_manager.py

96 lines
2.6 KiB
Python
Raw Normal View History

2024-03-24 09:43:34 +08:00
import os
from pydantic import Field
2024-04-19 00:45:57 +08:00
from .data import Database, LiteModel, Database
2024-03-24 09:43:34 +08:00
DATA_PATH = "data/liteyuki"
2024-04-22 23:55:33 +08:00
user_db: Database = Database(os.path.join(DATA_PATH, "users.ldb"))
group_db: Database = Database(os.path.join(DATA_PATH, "groups.ldb"))
plugin_db: Database = Database(os.path.join(DATA_PATH, "plugins.ldb"))
common_db: Database = Database(os.path.join(DATA_PATH, "common.ldb"))
2024-03-24 09:43:34 +08:00
2024-04-27 02:20:44 +08:00
# 内存数据库,临时用于存储数据
memory_database = {
}
2024-03-24 09:43:34 +08:00
class User(LiteModel):
TABLE_NAME:str = "user"
2024-03-26 17:14:41 +08:00
user_id: str = Field(str(), alias="user_id")
username: str = Field(str(), alias="username")
profile: dict[str, str] = Field(dict(), alias="profile")
enabled_plugins: list[str] = Field(list(), alias="enabled_plugins")
disabled_plugins: list[str] = Field(list(), alias="disabled_plugins")
2024-03-24 09:43:34 +08:00
2024-03-26 21:33:40 +08:00
class Group(LiteModel):
TABLE_NAME: str = "group_chat"
2024-03-24 09:43:34 +08:00
# Group是一个关键字所以这里用GroupChat
2024-03-26 17:14:41 +08:00
group_id: str = Field(str(), alias="group_id")
group_name: str = Field(str(), alias="group_name")
enabled_plugins: list[str] = Field([], alias="enabled_plugins")
disabled_plugins: list[str] = Field([], alias="disabled_plugins")
enable: bool = Field(True, alias="enable") # 群聊全局机器人是否启用
2024-03-24 09:43:34 +08:00
class InstalledPlugin(LiteModel):
TABLE_NAME: str = "installed_plugin"
2024-03-26 17:14:41 +08:00
module_name: str = Field(str(), alias="module_name")
version: str = Field(str(), alias="version")
2024-03-24 09:43:34 +08:00
class GlobalPlugin(LiteModel):
TABLE_NAME: str = "global_plugin"
2024-03-26 22:33:17 +08:00
liteyuki: bool = Field(True, alias="liteyuki") # 是否为LiteYuki插件
2024-03-26 17:14:41 +08:00
module_name: str = Field(str(), alias="module_name")
enabled: bool = Field(True, alias="enabled")
2024-03-24 09:43:34 +08:00
2024-03-29 14:58:24 +08:00
class StoredConfig(LiteModel):
TABLE_NAME :str= "stored_config"
2024-03-29 14:58:24 +08:00
config: dict = {}
2024-04-10 23:47:10 +08:00
class TempConfig(LiteModel):
"""储存临时键值对的表"""
TABLE_NAME: str = "temp_data"
2024-04-10 23:47:10 +08:00
data: dict = {}
2024-03-24 09:43:34 +08:00
def auto_migrate():
2024-03-26 21:33:40 +08:00
user_db.auto_migrate(User())
group_db.auto_migrate(Group())
2024-03-26 22:33:17 +08:00
plugin_db.auto_migrate(InstalledPlugin(), GlobalPlugin())
2024-04-10 23:47:10 +08:00
common_db.auto_migrate(GlobalPlugin(), StoredConfig(), TempConfig())
2024-04-27 02:20:44 +08:00
def set_memory_data(key: str, value) -> None:
"""
设置内存数据库的数据类似于redis
Args:
key:
value:
Returns:
"""
return memory_database.update({
key: value
})
def get_memory_data(key: str, default=None) -> any:
"""
获取内存数据库的数据,类似于redis
Args:
key:
default:
Returns:
"""
return memory_database.get(key, default)