11
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import json
|
||||
import shutil
|
||||
import asyncio
|
||||
import pathlib
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
from datetime import datetime
|
||||
|
||||
from astrbot.api.event import filter, AstrMessageEvent, MessageEventResult, MessageChain
|
||||
@@ -8,7 +11,7 @@ from astrbot.api.star import Context, Star, register, StarTools
|
||||
from astrbot.api import logger
|
||||
from astrbot.api import AstrBotConfig
|
||||
|
||||
from .core.async_mysql import AsyncMySQL
|
||||
from .core.aiosqlite import AsyncSQLite
|
||||
from .core.jx3_service import JX3Service
|
||||
|
||||
|
||||
@@ -25,10 +28,22 @@ class Jx3ApiPlugin(Star):
|
||||
#获取配置
|
||||
self.conf = config
|
||||
# 本地数据存储路径
|
||||
self.local_data_dir = StarTools.get_data_dir("astrbot_plugin_jx3")
|
||||
# api数据文件
|
||||
self.api_file_path = Path(__file__).parent / "api_config.json"
|
||||
# 读取文件内容
|
||||
local_data_dir = StarTools.get_data_dir("astrbot_plugin_jx3")
|
||||
# 插件数据文件路径
|
||||
data_file_path = Path(__file__).parent / "data"
|
||||
# --- 调用函数完成检查和复制 ---
|
||||
try:
|
||||
self.file_local_data = self.check_and_copy_db(
|
||||
local_data_dir=local_data_dir,
|
||||
db_filename="local_data.db",
|
||||
default_db_dir=data_file_path
|
||||
)
|
||||
except FileNotFoundError as e:
|
||||
# 处理默认文件丢失的严重错误
|
||||
logger.critical(f"插件初始化失败:{e}")
|
||||
raise # 中断初始化
|
||||
# 读取配置文件
|
||||
self.api_file_path = Path(__file__).parent / "data" / "api_config.json"
|
||||
with open(self.api_file_path, 'r', encoding='utf-8') as f:
|
||||
self.api_config = json.load(f)
|
||||
# 初始化数据
|
||||
@@ -38,25 +53,49 @@ class Jx3ApiPlugin(Star):
|
||||
|
||||
|
||||
async def initialize(self):
|
||||
"""可选择实现异步的插件初始化方法,当实例化该插件类之后会自动调用该方法。"""
|
||||
# 数据库配置
|
||||
db_config = {
|
||||
'host': '38.12.28.24',
|
||||
'port': 3306,
|
||||
'user': 'asrtbot',
|
||||
'password': 'qsc123456',
|
||||
'db': 'asrtbot',
|
||||
'charset': 'utf8mb4',
|
||||
'autocommit': True
|
||||
}
|
||||
"""可选择实现异步的插件初始化方法,当实例化该插件类之后会自动调用该方法。"""
|
||||
#创建类实例
|
||||
self.db = AsyncMySQL(db_config)
|
||||
self.db = AsyncSQLite(str(self.file_local_data))
|
||||
self.jx3fun = JX3Service(self.api_config,self.db)
|
||||
# 周期函数调用
|
||||
|
||||
|
||||
logger.info("jx3api插件创建实例完成")
|
||||
|
||||
|
||||
def check_and_copy_db(self, local_data_dir: Union[str, Path], db_filename: str, default_db_dir: Union[str, Path]) -> pathlib.Path:
|
||||
"""
|
||||
检查本地数据目录中是否存在指定的数据库文件。
|
||||
如果不存在,则从默认目录复制该文件。
|
||||
Args:
|
||||
local_data_dir: 目标数据库文件所在的文件夹路径。
|
||||
db_filename: 数据库文件的名称 (例如: 'local_data.db')。
|
||||
default_db_dir: 默认/源数据库文件所在的文件夹路径。
|
||||
Returns:
|
||||
最终的数据库文件的完整 pathlib.Path 对象。
|
||||
Raises:
|
||||
FileNotFoundError: 如果默认的源数据库文件不存在。
|
||||
"""
|
||||
# 目标路径
|
||||
target_dir = pathlib.Path(local_data_dir)
|
||||
target_file_path = target_dir / db_filename
|
||||
# 源文件路径
|
||||
source_file_path = pathlib.Path(default_db_dir) / db_filename
|
||||
# 假设默认文件名为 local_data.db
|
||||
if not target_file_path.exists():
|
||||
logger.warning(f"本地数据库文件 {target_file_path.name} 不存在,正在从默认位置复制...")
|
||||
# 1. 确保目标文件夹存在
|
||||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
# 2. 检查源文件是否存在
|
||||
if not source_file_path.exists():
|
||||
raise FileNotFoundError(f"默认数据库源文件未找到!请检查路径: {source_file_path}")
|
||||
# 3. 复制文件
|
||||
shutil.copy(source_file_path, target_file_path)
|
||||
logger.info(f"数据库文件已成功复制到: {target_file_path}")
|
||||
else:
|
||||
logger.info(f"本地数据库文件 {target_file_path} 已存在,跳过复制。")
|
||||
return target_file_path
|
||||
|
||||
|
||||
def inidata(self):
|
||||
"""数据初始化"""
|
||||
self.test_server = False
|
||||
@@ -114,7 +153,7 @@ class Jx3ApiPlugin(Star):
|
||||
if data["code"] == 200:
|
||||
yield event.plain_result(data["data"])
|
||||
else:
|
||||
yield event.plain_result("msg")
|
||||
yield event.plain_result(data["msg"])
|
||||
return
|
||||
except Exception as e:
|
||||
logger.error(f"功能函数执行错误: {e}")
|
||||
@@ -129,7 +168,7 @@ class Jx3ApiPlugin(Star):
|
||||
if data["code"] == 200:
|
||||
yield event.plain_result(data["data"])
|
||||
else:
|
||||
yield event.plain_result("msg")
|
||||
yield event.plain_result(data["msg"])
|
||||
return
|
||||
except Exception as e:
|
||||
logger.error(f"功能函数执行错误: {e}")
|
||||
@@ -295,7 +334,7 @@ class Jx3ApiPlugin(Star):
|
||||
|
||||
async def terminate(self):
|
||||
"""可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。"""
|
||||
await self.db.close_pool()
|
||||
await self.db.close()
|
||||
# 后台进程销毁
|
||||
self.kf_task.cancel()
|
||||
logger.info("jx3api插件已卸载/停用")
|
||||
Reference in New Issue
Block a user