diff --git a/core/async_mysql.py b/core/async_mysql.py deleted file mode 100644 index 66862ed..0000000 --- a/core/async_mysql.py +++ /dev/null @@ -1,93 +0,0 @@ -import aiomysql - -from astrbot.api import logger - -class AsyncMySQL: - def __init__(self, db_config: dict): - self.db_config = db_config - self.pool = None - - async def init_pool(self): - """初始化连接池""" - if self.pool is None: - self.pool = await aiomysql.create_pool(**self.db_config) - - async def close_pool(self): - """关闭连接池""" - if self.pool: - self.pool.close() - await self.pool.wait_closed() - self.pool = None - - async def fetch_one(self, sql: str, params=None): - """查询单条数据""" - await self.init_pool() - async with self.pool.acquire() as conn: # type: ignore - async with conn.cursor(aiomysql.DictCursor) as cursor: - await cursor.execute(sql, params or ()) - return await cursor.fetchone() - - async def fetch_all(self, sql: str, params=None): - """查询多条数据""" - await self.init_pool() - async with self.pool.acquire() as conn: # type: ignore - async with conn.cursor(aiomysql.DictCursor) as cursor: - await cursor.execute(sql, params or ()) - return await cursor.fetchall() - - - async def execute(self, sql: str, params=None): - """执行 SQL(insert/update/delete)""" - await self.init_pool() - async with self.pool.acquire() as conn: # type: ignore - async with conn.cursor() as cursor: - await cursor.execute(sql, params or ()) - await conn.commit() - return cursor.rowcount - - async def executemany(self, sql: str, params_list): - """批量执行 SQL""" - await self.init_pool() - async with self.pool.acquire() as conn: # type: ignore - async with conn.cursor() as cursor: - await cursor.executemany(sql, params_list) - await conn.commit() - return cursor.rowcount - - async def truncate_table(self, table_name: str): - """清空指定表""" - await self.init_pool() - async with self.pool.acquire() as conn: # type: ignore - async with conn.cursor() as cursor: - sql = f"TRUNCATE TABLE `{table_name}`" - await cursor.execute(sql) - await conn.commit() - return True - - # ---------------------------------------------------------------------- - # 新增:自动生成 SQL 的增删改功能 - # ---------------------------------------------------------------------- - - async def insert_record(self, table: str, data: dict): - """插入记录:data 是 dict""" - keys = ", ".join(f"`{k}`" for k in data.keys()) - placeholders = ", ".join(["%s"] * len(data)) - sql = f"INSERT INTO `{table}` ({keys}) VALUES ({placeholders})" - logger.info(f"Executing SQL: {sql}") - return await self.execute(sql, tuple(data.values())) - - async def update_record(self, table: str, data: dict, where: dict): - """更新记录:data、where 都是 dict""" - set_clause = ", ".join(f"`{k}`=%s" for k in data.keys()) - where_clause = " AND ".join(f"`{k}`=%s" for k in where.keys()) - - sql = f"UPDATE `{table}` SET {set_clause} WHERE {where_clause}" - - params = tuple(data.values()) + tuple(where.values()) - return await self.execute(sql, params) - - async def delete_record(self, table: str, where: dict): - """删除记录:where 是 dict""" - where_clause = " AND ".join(f"`{k}`=%s" for k in where.keys()) - sql = f"DELETE FROM `{table}` WHERE {where_clause}" - return await self.execute(sql, tuple(where.values())) \ No newline at end of file diff --git a/core/async_task.py b/core/async_task.py index 0cd0a69..d78941e 100644 --- a/core/async_task.py +++ b/core/async_task.py @@ -28,7 +28,7 @@ class AsyncTask: self.jx3fun = jx3fun - async def cycle_kaifjiankong(self): + async def cycle_kfjk(self): """开服监控后台程序""" # 获取配置信息 conf = self.conf.get("kfjk", {}) @@ -39,7 +39,6 @@ class AsyncTask: } self.kfjk_server_state = True # 上一次查询的状态 - self.kfjk_server_state_new = False # 最新查询的状态 if self.kfjk_conf["enable"]: logger.info(f"开服监控功能开启") @@ -58,7 +57,8 @@ class AsyncTask: for umo in self.kfjk_conf["umos"]: await self.context.send_message(umo, message_chain) self.kfjk_server_state = self.kfjk_server_state_new - await asyncio.sleep(self.kfjk_conf["time"]) # 休眠指定时间(分钟) + await asyncio.sleep(self.kfjk_conf["time"]) # 休眠指定时间 + async def get_kfjk_conf(self) -> str: """获取开服监控配置信息""" diff --git a/main.py b/main.py index 47d7901..3e62a03 100644 --- a/main.py +++ b/main.py @@ -58,7 +58,7 @@ class Jx3ApiPlugin(Star): self.jx3fun = JX3Service(self.api_config,self.db) self.at = AsyncTask(self.context, self.conf, self.jx3fun) # 周期函数调用 - self.kf_task = asyncio.create_task(self.at.cycle_kaifjiankong()) + self.kf_task = asyncio.create_task(self.at.cycle_kfjk()) logger.info("jx3api异步插件初始化完成") @@ -279,7 +279,8 @@ class Jx3ApiPlugin(Star): async def terminate(self): """可选择实现异步的插件销毁方法,当插件被卸载/停用时会调用。""" + # 关闭数据库连接 await self.db.close() - # 后台进程销毁 + # 后台z周期进程销毁 self.kf_task.cancel() logger.info("jx3api插件已卸载/停用") \ No newline at end of file