From a8d1fe19ff32c47cf829d062ab4e84bee18d87f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sun, 25 Dec 2022 15:56:52 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20Account=20Info=20Not=20Com?= =?UTF-8?q?pletely=20Remove=20from=20Database?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/cookies/repositories.py | 13 +++++++------ plugins/genshin/user.py | 11 ++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/cookies/repositories.py b/core/cookies/repositories.py index ae74f1ca..9067e26b 100644 --- a/core/cookies/repositories.py +++ b/core/cookies/repositories.py @@ -1,6 +1,7 @@ from typing import cast, List from sqlalchemy import select +from sqlalchemy.exc import NoResultFound from sqlmodel.ext.asyncio.session import AsyncSession from core.base.mysql import MySQL @@ -100,9 +101,9 @@ class CookiesRepository: else: raise RegionNotFoundError(region.name) results = await session.execute(statement) - db_cookies = results.unique().scalar_one() - if db_cookies: - await session.delete(db_cookies) - await session.commit() - else: - raise CookiesNotFoundError(user_id) + try: + db_cookies = results.unique().scalar_one() + except NoResultFound as exc: + raise CookiesNotFoundError(user_id) from exc + await session.delete(db_cookies) + await session.commit() diff --git a/plugins/genshin/user.py b/plugins/genshin/user.py index b90604f2..b7159ebe 100644 --- a/plugins/genshin/user.py +++ b/plugins/genshin/user.py @@ -8,6 +8,7 @@ from core.cookies import CookiesService from core.cookies.error import CookiesNotFoundError from core.cookies.models import Cookies from core.plugin import Plugin, handler, conversation +from core.sign import SignServices from core.user import UserService from core.user.error import UserNotFoundError from utils.decorators.error import error_callable @@ -30,9 +31,11 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation): self, user_service: UserService = None, cookies_service: CookiesService = None, + sign_service: SignServices = None, ): self.cookies_service = cookies_service self.user_service = user_service + self.sign_service = sign_service @conversation.entry_point @handler.command(command="deluser", filters=filters.ChatType.PRIVATE, block=True) @@ -62,9 +65,11 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation): if user_info.region == RegionEnum.HYPERION: uid = user_info.yuanshen_uid region_str = "米游社" + del_user_command_data.region = RegionEnum.HYPERION elif user_info.region == RegionEnum.HOYOLAB: uid = user_info.genshin_uid region_str = "HoYoLab" + del_user_command_data.region = RegionEnum.HOYOLAB else: await message.reply_text("数据非法") return ConversationHandler.END @@ -90,6 +95,10 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation): return ConversationHandler.END elif message.text == "确认": del_user_command_data: DelUserCommandData = context.chat_data.get("del_user_command_data") + sign = await self.sign_service.get_by_user_id(user.id) + if sign: + await self.sign_service.remove(sign) + logger.success("用户 %s[%s] 从数据库删除定时签到成功", user.full_name, user.id) try: await self.user_service.del_user_by_id(user.id) except UserNotFoundError: @@ -102,7 +111,7 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation): try: await self.cookies_service.del_cookies(user.id, del_user_command_data.region) except CookiesNotFoundError: - logger.info("用户 %s[%s] Cookies 不存在", user.full_name, user.id) + logger.warning("用户 %s[%s] Cookies 不存在", user.full_name, user.id) else: logger.success("用户 %s[%s] 从数据库删除Cookies成功", user.full_name, user.id) await message.reply_text("删除成功")