From 6302ba72b17a7f6568fa87f0696c086d355a227e Mon Sep 17 00:00:00 2001
From: omg-xtao <100690902+omg-xtao@users.noreply.github.com>
Date: Wed, 12 Oct 2022 21:22:41 +0800
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=E8=87=AA?=
=?UTF-8?q?=E5=8A=A8=E7=AD=BE=E5=88=B0=E7=BB=9F=E8=AE=A1=E4=BF=A1=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
plugins/genshin/sign.py | 1 +
plugins/jobs/sign.py | 24 ++++++++++++++----------
plugins/system/sign_status.py | 31 +++++++++++++++++++++++++++++++
3 files changed, 46 insertions(+), 10 deletions(-)
create mode 100644 plugins/system/sign_status.py
diff --git a/plugins/genshin/sign.py b/plugins/genshin/sign.py
index 660f6e50..f4e951cb 100644
--- a/plugins/genshin/sign.py
+++ b/plugins/genshin/sign.py
@@ -233,6 +233,7 @@ class Sign(Plugin, BasePlugin):
if user.chat_id == chat_id:
return "自动签到已经开启过了"
user.chat_id = chat_id
+ user.status = SignStatusEnum.STATUS_SUCCESS
await self.sign_service.update(user)
return "修改自动签到通知对话成功"
elif method == "关闭":
diff --git a/plugins/jobs/sign.py b/plugins/jobs/sign.py
index e498a014..e4d90abc 100644
--- a/plugins/jobs/sign.py
+++ b/plugins/jobs/sign.py
@@ -16,6 +16,8 @@ from core.sign.models import SignStatusEnum
from core.sign.services import SignServices
from core.user import UserService
from plugins.genshin.sign import Sign
+from plugins.system.errorhandler import notice_chat_id
+from plugins.system.sign_status import SignStatus
from utils.helpers import get_genshin_client
from utils.log import logger
@@ -85,18 +87,17 @@ class SignJob(Plugin):
@job.run_daily(time=datetime.time(hour=0, minute=1, second=0), name="SignJob")
async def sign(self, context: CallbackContext):
- if context.job.name == "SignJob":
- logger.info("正在执行自动签到")
- if context.job.name == "SignAgainJob":
- logger.info("正在执行自动重签")
+ logger.info("正在执行自动签到" if context.job.name == "SignJob" else "正在执行自动重签")
sign_list = await self.sign_service.get_all()
for sign_db in sign_list:
user_id = sign_db.user_id
- if sign_db.status != SignStatusEnum.STATUS_SUCCESS:
- if sign_db.status == SignStatusEnum.TIMEOUT_ERROR:
- if context.job.name == "SignAgainJob":
- logger.info(f"用户 [{user_id}] 即将执行重签")
- else:
+ if sign_db.status in [SignStatusEnum.INVALID_COOKIES, SignStatusEnum.FORBIDDEN]:
+ continue
+ if context.job.name == "SignJob":
+ if sign_db.status not in [SignStatusEnum.STATUS_SUCCESS, SignStatusEnum.ALREADY_CLAIMED]:
+ continue
+ elif context.job.name == "SignAgainJob":
+ if sign_db.status in [SignStatusEnum.STATUS_SUCCESS, SignStatusEnum.ALREADY_CLAIMED]:
continue
try:
text = await self.single_sign(user_id)
@@ -142,6 +143,9 @@ class SignJob(Plugin):
continue
sign_db.time_updated = datetime.datetime.now()
await self.sign_service.update(sign_db)
- logger.info("执行自动签到完成")
+ logger.info("执行自动签到完成" if context.job.name == "SignJob" else "执行自动重签完成")
if context.job.name == "SignJob":
context.job_queue.run_once(self.sign, when=datetime.time(hour=0, minute=1, second=0), name="SignAgainJob")
+ elif context.job.name == "SignAgainJob":
+ text = await SignStatus.get_sign_status(self.sign_service)
+ await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
diff --git a/plugins/system/sign_status.py b/plugins/system/sign_status.py
new file mode 100644
index 00000000..6da58057
--- /dev/null
+++ b/plugins/system/sign_status.py
@@ -0,0 +1,31 @@
+from telegram import Update
+from telegram.ext import CommandHandler, CallbackContext
+
+from core.plugin import Plugin, handler
+from core.sign import SignServices
+from utils.decorators.admins import bot_admins_rights_check
+from utils.log import logger
+
+
+class SignStatus(Plugin):
+ def __init__(self, sign_service: SignServices = None):
+ self.sign_service = sign_service
+
+ @staticmethod
+ async def get_sign_status(sign_service: SignServices) -> str:
+ sign_db = await sign_service.get_all()
+ names = ["签到成功", "Cookie 无效", "提前签到", "触发验证码", "API异常", "请求超时", "请求失败", "通知失败"]
+ values = [0, 0, 0, 0, 0, 0, 0, 0]
+ for sign in sign_db:
+ values[sign.status.value] += 1
+ text = f"自动签到统计信息\n\n总人数:{len(sign_db)}
\n"
+ return text + "\n".join(f"{name}: {value}
" for name, value in zip(names, values))
+
+ @handler(CommandHandler, command="sign_status", block=False)
+ @bot_admins_rights_check
+ async def sign_status(self, update: Update, _: CallbackContext):
+ user = update.effective_user
+ logger.info(f"用户 {user.full_name}[{user.id}] sign_status 命令请求")
+ message = update.effective_message
+ text = await self.get_sign_status(self.sign_service)
+ await message.reply_text(text, parse_mode="html", quote=True)