feat: 持久记忆和代码优化、函数工具筛选

This commit is contained in:
2025-12-10 17:21:43 +08:00
parent 7d3ef70093
commit e0a38eb6f2
87 changed files with 2179 additions and 241 deletions

View File

@@ -78,10 +78,11 @@ class DeerCheckin(PluginBase):
async def _init_db(self):
"""初始化数据库"""
conn = None
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS checkin (
user_id TEXT NOT NULL,
@@ -90,40 +91,44 @@ class DeerCheckin(PluginBase):
PRIMARY KEY (user_id, checkin_date)
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS metadata (
key TEXT PRIMARY KEY,
value TEXT
)
''')
conn.commit()
conn.close()
logger.info("鹿打卡数据库初始化成功")
except Exception as e:
logger.error(f"数据库初始化失败: {e}")
finally:
if conn:
conn.close()
async def _monthly_cleanup(self):
"""月度数据清理"""
current_month = date.today().strftime("%Y-%m")
conn = None
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT value FROM metadata WHERE key = 'last_cleanup_month'")
result = cursor.fetchone()
if not result or result[0] != current_month:
cursor.execute("DELETE FROM checkin WHERE strftime('%Y-%m', checkin_date) != ?", (current_month,))
cursor.execute("INSERT OR REPLACE INTO metadata (key, value) VALUES (?, ?)",
("last_cleanup_month", current_month))
conn.commit()
logger.info(f"已执行月度清理,现在是 {current_month}")
conn.close()
except Exception as e:
logger.error(f"月度数据清理失败: {e}")
finally:
if conn:
conn.close()
@on_text_message(priority=90)
async def handle_deer_message(self, bot: WechatHookClient, message: dict):
@@ -192,32 +197,35 @@ class DeerCheckin(PluginBase):
nickname = user_info["nickName"]["string"]
except:
pass
deer_count = content.count("🦌")
today_str = date.today().strftime("%Y-%m-%d")
conn = None
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO checkin (user_id, checkin_date, deer_count)
VALUES (?, ?, ?)
ON CONFLICT(user_id, checkin_date)
DO UPDATE SET deer_count = deer_count + excluded.deer_count
''', (user_id, today_str, deer_count))
conn.commit()
conn.close()
logger.info(f"用户 {nickname} ({user_id}) 打卡成功,记录了 {deer_count} 个🦌")
# 生成并发送日历
await self._generate_and_send_calendar(bot, from_wxid, user_id, nickname)
except Exception as e:
logger.error(f"记录打卡数据失败: {e}")
await bot.send_text(from_wxid, "打卡失败,数据库出错了 >_<")
finally:
if conn:
conn.close()
async def _handle_calendar(self, bot: WechatHookClient, from_wxid: str, user_id: str, nickname: str):
"""处理查看日历"""
@@ -254,23 +262,27 @@ class DeerCheckin(PluginBase):
# 执行补签
target_date = date(today.year, today.month, day_to_checkin)
target_date_str = target_date.strftime("%Y-%m-%d")
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO checkin (user_id, checkin_date, deer_count)
VALUES (?, ?, ?)
ON CONFLICT(user_id, checkin_date)
DO UPDATE SET deer_count = deer_count + excluded.deer_count
''', (user_id, target_date_str, deer_count))
conn.commit()
conn.close()
await bot.send_text(from_wxid, f"补签成功!已为 {today.month}{day_to_checkin}日 增加了 {deer_count} 个鹿")
await self._generate_and_send_calendar(bot, from_wxid, user_id, nickname)
conn = None
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO checkin (user_id, checkin_date, deer_count)
VALUES (?, ?, ?)
ON CONFLICT(user_id, checkin_date)
DO UPDATE SET deer_count = deer_count + excluded.deer_count
''', (user_id, target_date_str, deer_count))
conn.commit()
await bot.send_text(from_wxid, f"补签成功!已为 {today.month}{day_to_checkin}日 增加了 {deer_count} 个鹿")
await self._generate_and_send_calendar(bot, from_wxid, user_id, nickname)
finally:
if conn:
conn.close()
except Exception as e:
logger.error(f"补签失败: {e}")
await bot.send_text(from_wxid, "补签失败,数据库出错了 >_<")
@@ -302,6 +314,7 @@ class DeerCheckin(PluginBase):
async def _generate_and_send_calendar(self, bot: WechatHookClient, from_wxid: str, user_id: str, nickname: str):
"""生成并发送日历"""
conn = None
try:
current_year = date.today().year
current_month = date.today().month
@@ -310,15 +323,14 @@ class DeerCheckin(PluginBase):
# 查询打卡记录
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute(
"SELECT checkin_date, deer_count FROM checkin WHERE user_id = ? AND strftime('%Y-%m', checkin_date) = ?",
(user_id, current_month_str)
)
rows = cursor.fetchall()
conn.close()
if not rows:
await bot.send_text(from_wxid, "您本月还没有打卡记录哦,发送🦌开始第一次打卡吧!")
return
@@ -336,12 +348,12 @@ class DeerCheckin(PluginBase):
image_path = await self._create_calendar_image(
user_id, nickname, current_year, current_month, checkin_records, total_deer
)
if image_path:
# 发送图片
data = {"to_wxid": from_wxid, "file": str(image_path)}
await bot._send_data_async(11040, data)
# 不删除临时文件
else:
# 发送文本版本
@@ -351,6 +363,9 @@ class DeerCheckin(PluginBase):
except Exception as e:
logger.error(f"生成日历失败: {e}")
await bot.send_text(from_wxid, "生成日历时发生错误 >_<")
finally:
if conn:
conn.close()
async def _create_calendar_image(self, user_id: str, nickname: str, year: int, month: int, checkin_data: Dict, total_deer: int) -> Optional[str]:
"""创建日历图片"""