2022-11-17 08:07:18 +00:00
|
|
|
from typing import Optional, Tuple
|
2022-07-31 05:25:02 +00:00
|
|
|
|
2023-08-21 14:42:44 +00:00
|
|
|
from telegram import Update
|
2022-10-10 05:34:06 +00:00
|
|
|
from telegram.constants import ChatAction
|
2023-12-24 13:26:36 +00:00
|
|
|
from telegram.ext import CallbackContext
|
|
|
|
from telegram.ext import filters
|
2022-11-28 08:38:49 +00:00
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-31 05:25:02 +00:00
|
|
|
|
2022-10-10 05:34:06 +00:00
|
|
|
from core.config import config
|
2023-03-16 15:00:04 +00:00
|
|
|
from core.handler.callbackqueryhandler import CallbackQueryHandler
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2023-07-23 15:46:11 +00:00
|
|
|
from core.services.task.models import Task as SignUser, TaskStatusEnum
|
|
|
|
from core.services.task.services import SignServices
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.services.users.services import UserAdminService
|
2024-11-18 13:58:25 +00:00
|
|
|
from plugins.tools.genshin import GenshinHelper, PlayerNotFoundError, CookiesNotFoundError
|
2023-03-14 01:27:22 +00:00
|
|
|
from plugins.tools.sign import SignSystem, NeedChallenge
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-31 05:25:02 +00:00
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
class Sign(Plugin):
|
2022-10-30 13:35:53 +00:00
|
|
|
"""每日签到"""
|
|
|
|
|
|
|
|
CHECK_SERVER, COMMAND_RESULT = range(10400, 10402)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-03-14 01:27:22 +00:00
|
|
|
genshin_helper: GenshinHelper,
|
|
|
|
sign_service: SignServices,
|
|
|
|
user_admin_service: UserAdminService,
|
|
|
|
sign_system: SignSystem,
|
2022-10-30 13:35:53 +00:00
|
|
|
):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.user_admin_service = user_admin_service
|
2022-10-30 13:35:53 +00:00
|
|
|
self.sign_service = sign_service
|
2023-03-14 01:27:22 +00:00
|
|
|
self.sign_system = sign_system
|
|
|
|
self.genshin_helper = genshin_helper
|
2022-07-31 05:25:02 +00:00
|
|
|
|
2024-11-18 13:38:50 +00:00
|
|
|
async def _process_auto_sign(self, user_id: int, player_id: int, offset: int, chat_id: int, method: str) -> str:
|
2024-11-18 13:58:25 +00:00
|
|
|
try:
|
|
|
|
c = await self.genshin_helper.get_genshin_client(user_id, player_id=player_id, offset=offset)
|
|
|
|
player_id = c.player_id
|
|
|
|
except (PlayerNotFoundError, CookiesNotFoundError):
|
2024-06-07 15:22:30 +00:00
|
|
|
return config.notice.user_not_found
|
2024-11-18 13:58:25 +00:00
|
|
|
user: SignUser = await self.sign_service.get_by_user_id(user_id, player_id)
|
2022-08-06 07:21:18 +00:00
|
|
|
if user:
|
|
|
|
if method == "关闭":
|
|
|
|
await self.sign_service.remove(user)
|
2024-11-18 13:58:25 +00:00
|
|
|
return f"UID {player_id} 关闭自动签到成功"
|
2023-03-14 01:27:22 +00:00
|
|
|
if method == "开启":
|
2022-08-06 07:21:18 +00:00
|
|
|
if user.chat_id == chat_id:
|
2024-11-18 13:58:25 +00:00
|
|
|
return f"UID {player_id} 自动签到已经开启过了"
|
2022-08-06 07:21:18 +00:00
|
|
|
user.chat_id = chat_id
|
2023-07-23 15:46:11 +00:00
|
|
|
user.status = TaskStatusEnum.STATUS_SUCCESS
|
2022-08-06 07:21:18 +00:00
|
|
|
await self.sign_service.update(user)
|
2024-11-18 13:58:25 +00:00
|
|
|
return f"UID {player_id} 修改自动签到通知对话成功"
|
2022-08-06 07:21:18 +00:00
|
|
|
elif method == "关闭":
|
2024-11-18 13:58:25 +00:00
|
|
|
return f"UID {player_id} 您还没有开启自动签到"
|
2022-08-06 07:21:18 +00:00
|
|
|
elif method == "开启":
|
2024-11-18 13:58:25 +00:00
|
|
|
user = self.sign_service.create(user_id, player_id, chat_id, TaskStatusEnum.STATUS_SUCCESS)
|
2022-08-06 07:21:18 +00:00
|
|
|
await self.sign_service.add(user)
|
2024-11-18 13:58:25 +00:00
|
|
|
return f"UID {player_id} 开启自动签到成功"
|
2022-08-06 07:21:18 +00:00
|
|
|
|
2024-03-25 12:48:19 +00:00
|
|
|
@handler.command(command="sign", cookie=True, block=False)
|
|
|
|
@handler.message(filters=filters.Regex("^每日签到(.*)"), cookie=True, block=False)
|
2023-12-24 13:26:36 +00:00
|
|
|
@handler.command(command="start", filters=filters.Regex("sign$"), block=False)
|
2022-07-31 05:25:02 +00:00
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
2024-03-10 12:40:26 +00:00
|
|
|
user_id = await self.get_real_user_id(update)
|
2024-11-18 13:38:50 +00:00
|
|
|
uid, offset = self.get_real_uid_or_offset(update)
|
2022-09-08 01:08:37 +00:00
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
args = self.get_args(context)
|
2022-10-30 13:35:53 +00:00
|
|
|
validate: Optional[str] = None
|
2022-08-06 07:21:18 +00:00
|
|
|
if len(args) >= 1:
|
|
|
|
msg = None
|
|
|
|
if args[0] == "开启自动签到":
|
2024-03-10 12:40:26 +00:00
|
|
|
if await self.user_admin_service.is_admin(user_id):
|
2024-11-18 13:38:50 +00:00
|
|
|
msg = await self._process_auto_sign(user_id, uid, offset, message.chat_id, "开启")
|
2022-09-09 17:06:23 +00:00
|
|
|
else:
|
2024-11-18 13:38:50 +00:00
|
|
|
msg = await self._process_auto_sign(user_id, uid, offset, user_id, "开启")
|
2022-08-06 07:21:18 +00:00
|
|
|
elif args[0] == "关闭自动签到":
|
2024-11-18 13:38:50 +00:00
|
|
|
msg = await self._process_auto_sign(user_id, uid, offset, message.chat_id, "关闭")
|
|
|
|
elif args[0] != "sign" and not args[0].startswith("@"):
|
2022-10-30 08:46:07 +00:00
|
|
|
validate = args[0]
|
2022-08-06 07:21:18 +00:00
|
|
|
if msg:
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "自动签到命令请求 || 参数 %s", args[0])
|
2022-08-06 07:21:18 +00:00
|
|
|
reply_message = await message.reply_text(msg)
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
2023-03-26 08:12:11 +00:00
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-08-06 07:21:18 +00:00
|
|
|
return
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "每日签到命令请求")
|
2022-07-31 05:25:02 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message)
|
2022-07-31 05:25:02 +00:00
|
|
|
try:
|
2024-11-18 13:38:50 +00:00
|
|
|
async with self.genshin_helper.genshin(user_id, player_id=uid, offset=offset) as client:
|
2023-07-18 09:29:31 +00:00
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
_, challenge = await self.sign_system.get_challenge(client.player_id)
|
|
|
|
if validate:
|
|
|
|
_, challenge = await self.sign_system.get_challenge(client.player_id)
|
|
|
|
if challenge:
|
|
|
|
sign_text = await self.sign_system.start_sign(client, challenge=challenge, validate=validate)
|
|
|
|
else:
|
2024-06-10 16:58:39 +00:00
|
|
|
reply_message = await message.reply_text("请求已经过期")
|
2023-07-18 09:29:31 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
self.add_delete_message_job(reply_message)
|
|
|
|
return
|
2022-11-18 08:35:14 +00:00
|
|
|
else:
|
2023-07-18 09:29:31 +00:00
|
|
|
sign_text = await self.sign_system.start_sign(client)
|
2024-06-10 16:58:39 +00:00
|
|
|
reply_message = await message.reply_text(sign_text)
|
2022-07-31 05:25:02 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message)
|
2022-10-30 13:35:53 +00:00
|
|
|
except NeedChallenge as exc:
|
2023-03-14 01:27:22 +00:00
|
|
|
button = await self.sign_system.get_challenge_button(
|
|
|
|
context.bot.username,
|
|
|
|
exc.uid,
|
2024-03-10 12:40:26 +00:00
|
|
|
user_id,
|
2023-03-14 01:27:22 +00:00
|
|
|
exc.gt,
|
|
|
|
exc.challenge,
|
|
|
|
not filters.ChatType.PRIVATE.filter(message),
|
2022-10-30 13:35:53 +00:00
|
|
|
)
|
|
|
|
reply_message = await message.reply_text(
|
2024-03-16 01:44:15 +00:00
|
|
|
"签到失败,触发验证码风控,请尝试点击下方按钮重新签到",
|
|
|
|
reply_markup=button,
|
2022-10-30 13:35:53 +00:00
|
|
|
)
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message)
|
2022-10-30 11:37:57 +00:00
|
|
|
|
2023-12-24 13:26:36 +00:00
|
|
|
@handler.command(command="start", filters=filters.Regex(r" challenge_sign_(.*)"), block=False)
|
|
|
|
async def command_challenge(self, update: Update, context: CallbackContext) -> None:
|
|
|
|
user = update.effective_user
|
2024-11-18 13:38:50 +00:00
|
|
|
uid, offset = self.get_real_uid_or_offset(update)
|
2023-12-24 13:26:36 +00:00
|
|
|
message = update.effective_message
|
|
|
|
args = context.args
|
|
|
|
_data = args[0].split("_")
|
|
|
|
validate = _data[2]
|
|
|
|
logger.info("用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id)
|
|
|
|
try:
|
2024-11-18 13:38:50 +00:00
|
|
|
async with self.genshin_helper.genshin(user.id, player_id=uid, offset=offset) as client:
|
2023-12-24 13:26:36 +00:00
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
_, challenge = await self.sign_system.get_challenge(client.player_id)
|
|
|
|
if not challenge:
|
2024-06-10 16:58:39 +00:00
|
|
|
await message.reply_text("验证请求已过期。")
|
2023-12-24 13:26:36 +00:00
|
|
|
return
|
|
|
|
sign_text = await self.sign_system.start_sign(client, challenge=challenge, validate=validate)
|
2024-06-10 16:58:39 +00:00
|
|
|
await message.reply_text(sign_text)
|
2023-12-24 13:26:36 +00:00
|
|
|
except NeedChallenge:
|
2024-06-10 16:58:39 +00:00
|
|
|
await message.reply_text("回调错误,请重新签到")
|
2023-12-24 13:26:36 +00:00
|
|
|
|
2022-10-30 11:37:57 +00:00
|
|
|
@handler(CallbackQueryHandler, pattern=r"^sign\|", block=False)
|
2023-03-14 01:27:22 +00:00
|
|
|
async def sign_gen_link(self, update: Update, context: CallbackContext) -> None:
|
2022-10-30 11:37:57 +00:00
|
|
|
callback_query = update.callback_query
|
|
|
|
user = callback_query.from_user
|
|
|
|
|
|
|
|
async def get_sign_callback(callback_query_data: str) -> Tuple[int, int]:
|
|
|
|
_data = callback_query_data.split("|")
|
|
|
|
_user_id = int(_data[1])
|
|
|
|
_uid = int(_data[2])
|
2023-03-14 01:27:22 +00:00
|
|
|
logger.debug("get_sign_callback 函数返回 user_id[%s] uid[%s]", _user_id, _uid)
|
2022-10-30 11:37:57 +00:00
|
|
|
return _user_id, _uid
|
|
|
|
|
|
|
|
user_id, uid = await get_sign_callback(callback_query.data)
|
|
|
|
if user.id != user_id:
|
2023-01-07 20:01:28 +00:00
|
|
|
await callback_query.answer(text="这不是你的按钮!\n" + config.notice.user_mismatch, show_alert=True)
|
2022-10-30 11:37:57 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
_, challenge = await self.sign_system.get_challenge(uid)
|
2022-10-30 11:37:57 +00:00
|
|
|
if not challenge:
|
|
|
|
await callback_query.answer(text="验证请求已经过期,请重新发起签到!", show_alert=True)
|
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
await callback_query.answer(url=create_deep_linked_url(context.bot.username, "sign"))
|