mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 06:12:53 +08:00
* ✨ feat(env): 支持git更新 * ✨ feat(aliyun): 更新阿里云URL构建逻辑,支持组织名称并优化令牌解码处理 * ✨ feat(config): 修改错误提示信息,更新基础配置文件名称为.env.example * ⚡ 插件商店支持aliyun * ✨ feat(store): 优化插件数据获取逻辑,合并插件列表和额外插件列表 * 🐛 修复非git仓库的初始化更新 * ✨ feat(update): 增强更新提示信息,添加非git源的变更文件说明 * 🎨 代码格式化 * ✨ webui与resources支持git更新 * ✨ feat(update): 更新webui路径处理逻辑 * Fix/test_runwork (#2001) * fix(test): 修复测试工作流 - 修改自动更新模块中的导入路径 - 更新插件商店模块中的插件信息获取逻辑 - 优化插件添加、更新和移除流程 - 统一插件相关错误信息的格式 - 调整测试用例以适应新的插件管理逻辑 * test(builtin_plugins): 重构插件商店相关测试 - 移除 jsd 相关测试用例,只保留 gh(GitHub)的测试 - 删除了 test_plugin_store.py 文件,清理了插件商店的测试 - 更新了 test_search_plugin.py 中的插件版本号 - 调整了 test_update_plugin.py 中的已加载插件版本 - 移除了 StoreManager 类中的 is_external 变量 - 更新了 RepoFileManager 类中的文件获取逻辑,优先使用 GitHub * ✨ feat(submodule): 添加子模块管理功能,支持子模块的初始化、更新和信息获取 * ✨ feat(update): 移除资源管理器,重构更新逻辑,支持通过ZhenxunRepoManager进行资源和Web UI的更新 * test(auto_update): 修改更新检测消息格式 (#2003) - 移除了不必要的版本号后缀(如 "-e6f17c4") - 统一了版本更新消息的格式,删除了冗余信息 * 🐛 修复web zip更新路径问题 * ⚡ 文件获取优化使用ali * Fix/test (#2008) * test: 修复bot测试 - 在 test_check_update.py 中跳过两个测试函数 - 移除 test_check.py 中的 mocked_api 参数和相关调用 - 删除 test_add_plugin.py 中的多个测试函数 - 移除 test_remove_plugin.py 中的 mocked_api 参数和相关调用 - 删除 test_search_plugin.py 中的多个测试函数 - 移除 test_update_all_plugin.py 和 test_update_plugin.py 中的 mocked_api 参数和相关调用 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * 修复res zip更新路径问题 * 🐛 修复zhenxun更新zip占用问题 * ✨ feat(update): 优化资源更新逻辑,调整更新路径和消息处理 --------- Co-authored-by: molanp <104612722+molanp@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
174 lines
6.5 KiB
Python
174 lines
6.5 KiB
Python
from datetime import datetime
|
|
import uuid
|
|
|
|
import nonebot
|
|
from nonebot.adapters import Bot
|
|
from nonebot.drivers import Driver
|
|
from tortoise import Tortoise
|
|
from tortoise.exceptions import IntegrityError, OperationalError
|
|
import ujson as json
|
|
|
|
from zhenxun.models.bot_connect_log import BotConnectLog
|
|
from zhenxun.models.bot_console import BotConsole
|
|
from zhenxun.models.goods_info import GoodsInfo
|
|
from zhenxun.models.group_member_info import GroupInfoUser
|
|
from zhenxun.models.sign_user import SignUser
|
|
from zhenxun.models.user_console import UserConsole
|
|
from zhenxun.services.log import logger
|
|
from zhenxun.utils.decorator.shop import shop_register
|
|
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
|
from zhenxun.utils.manager.zhenxun_repo_manager import ZhenxunRepoManager
|
|
from zhenxun.utils.platform import PlatformUtils
|
|
|
|
driver: Driver = nonebot.get_driver()
|
|
|
|
|
|
@driver.on_bot_connect
|
|
async def _(bot: Bot):
|
|
logger.debug(f"Bot: {bot.self_id} 建立连接...")
|
|
await BotConnectLog.create(
|
|
bot_id=bot.self_id, platform=bot.adapter, connect_time=datetime.now(), type=1
|
|
)
|
|
if not await BotConsole.exists(bot_id=bot.self_id):
|
|
try:
|
|
await BotConsole.create(
|
|
bot_id=bot.self_id, platform=PlatformUtils.get_platform(bot)
|
|
)
|
|
except IntegrityError as e:
|
|
logger.warning(f"记录bot: {bot.self_id} 数据已存在...", e=e)
|
|
|
|
|
|
@driver.on_bot_disconnect
|
|
async def _(bot: Bot):
|
|
logger.debug(f"Bot: {bot.self_id} 断开连接...")
|
|
try:
|
|
await BotConnectLog.create(
|
|
bot_id=bot.self_id,
|
|
platform=bot.adapter,
|
|
connect_time=datetime.now(),
|
|
type=0,
|
|
)
|
|
except Exception as e:
|
|
logger.warning(f"记录bot: {bot.self_id} 断开连接失败", e=e)
|
|
|
|
|
|
SIGN_SQL = """
|
|
SELECT user_id, checkin_count, add_probability, specify_probability, impression
|
|
FROM (
|
|
SELECT
|
|
t1.user_id,
|
|
t1.checkin_count,
|
|
t1.add_probability,
|
|
t1.specify_probability,
|
|
t1.impression,
|
|
ROW_NUMBER() OVER(PARTITION BY t1.user_id ORDER BY t1.impression DESC) AS rn
|
|
FROM sign_group_users t1
|
|
INNER JOIN (
|
|
SELECT user_id, MAX(impression) AS max_impression
|
|
FROM sign_group_users
|
|
GROUP BY user_id
|
|
) t2 ON t2.user_id = t1.user_id AND t2.max_impression = t1.impression
|
|
) t
|
|
WHERE rn = 1
|
|
"""
|
|
|
|
BAG_SQL = """
|
|
select t1.user_id, t1.gold, t1.property
|
|
from bag_users t1
|
|
join (
|
|
select user_id, max(t2.gold) as max_gold
|
|
from bag_users t2
|
|
group by user_id
|
|
) t on t.user_id = t1.user_id and t.max_gold = t1.gold
|
|
"""
|
|
|
|
|
|
@PriorityLifecycle.on_startup(priority=5)
|
|
async def _():
|
|
if not ZhenxunRepoManager.check_resources_exists():
|
|
await ZhenxunRepoManager.resources_update()
|
|
"""签到与用户的数据迁移"""
|
|
if goods_list := await GoodsInfo.filter(uuid__isnull=True).all():
|
|
for goods in goods_list:
|
|
goods.uuid = uuid.uuid1() # type: ignore
|
|
await GoodsInfo.bulk_update(goods_list, ["uuid"], 10)
|
|
await shop_register.load_register()
|
|
if (
|
|
not await UserConsole.annotate().count()
|
|
and not await SignUser.annotate().count()
|
|
):
|
|
try:
|
|
group_user = []
|
|
try:
|
|
group_user = await GroupInfoUser.filter(uid__isnull=False).all()
|
|
except Exception as e:
|
|
logger.warning("获取GroupInfoUser数据uid失败...", e=e)
|
|
user2uid = {u.user_id: u.uid for u in group_user}
|
|
db = Tortoise.get_connection("default")
|
|
old_sign_list = await db.execute_query_dict(SIGN_SQL)
|
|
old_bag_list = await db.execute_query_dict(BAG_SQL)
|
|
goods = {
|
|
g["goods_name"]: g["uuid"]
|
|
for g in await GoodsInfo.annotate().values("goods_name", "uuid")
|
|
}
|
|
create_list = []
|
|
sign_id_list = []
|
|
max_uid = max(user2uid.values()) + 1 if user2uid else 0
|
|
for old_sign in old_sign_list:
|
|
sign_id_list.append(old_sign["user_id"])
|
|
if old_bag := [
|
|
b for b in old_bag_list if b["user_id"] == old_sign["user_id"]
|
|
]:
|
|
old_bag = old_bag[0]
|
|
property = json.loads(old_bag["property"])
|
|
props = {}
|
|
if property:
|
|
for name, num in property.items():
|
|
if name in goods:
|
|
props[goods[name]] = num
|
|
create_list.append(
|
|
UserConsole(
|
|
user_id=old_sign["user_id"],
|
|
platform="qq",
|
|
uid=user2uid.get(old_sign["user_id"]) or max_uid,
|
|
props=props,
|
|
gold=old_bag["gold"],
|
|
)
|
|
)
|
|
if not user2uid.get(old_sign["user_id"]):
|
|
max_uid += 1
|
|
else:
|
|
create_list.append(
|
|
UserConsole(
|
|
user_id=old_sign["user_id"], platform="qq", uid=max_uid
|
|
)
|
|
)
|
|
max_uid += 1
|
|
if create_list:
|
|
logger.info("开始迁移用户数据...")
|
|
await UserConsole.bulk_create(create_list, 10)
|
|
logger.info("迁移用户数据完成!")
|
|
create_list.clear()
|
|
uc_dict = {u.user_id: u for u in await UserConsole.all()}
|
|
for old_sign in old_sign_list:
|
|
user_console = uc_dict.get(
|
|
old_sign["user_id"]
|
|
) or await UserConsole.get_user(old_sign["user_id"], "qq")
|
|
create_list.append(
|
|
SignUser(
|
|
user_id=old_sign["user_id"],
|
|
user_console=user_console,
|
|
platform="qq",
|
|
sign_count=old_sign["checkin_count"],
|
|
impression=old_sign["impression"],
|
|
add_probability=old_sign["add_probability"],
|
|
specify_probability=old_sign["specify_probability"],
|
|
)
|
|
)
|
|
if create_list:
|
|
logger.info("开始迁移签到数据...")
|
|
await SignUser.bulk_create(create_list, 10)
|
|
logger.info("迁移签到数据完成!")
|
|
except OperationalError as e:
|
|
logger.warning("数据迁移", e=e)
|