Seperate start command

This commit is contained in:
omg-xtao 2023-12-24 21:26:36 +08:00 committed by GitHub
parent d9de6b78f2
commit 949f9eedc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 150 deletions

View File

@ -63,6 +63,7 @@ class BindAccountPlugin(Plugin.Conversation):
@conversation.entry_point
@handler.command(command="setuid", filters=filters.ChatType.PRIVATE, block=False)
@handler.command(command="start", filters=filters.Regex("set_uid$"), block=False)
async def command_start(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> int:
user = update.effective_user
message = update.effective_message

View File

@ -127,6 +127,7 @@ class AccountCookiesPlugin(Plugin.Conversation):
@conversation.entry_point
@handler.command(command="setcookie", filters=filters.ChatType.PRIVATE, block=False)
@handler.command(command="setcookies", filters=filters.ChatType.PRIVATE, block=False)
@handler.command(command="start", filters=filters.Regex("set_cookie$"), block=False)
async def command_start(self, update: Update, context: CallbackContext) -> int:
user = update.effective_user
message = update.effective_message

View File

@ -1,78 +1,21 @@
from typing import Optional, TYPE_CHECKING
from simnet.errors import RedemptionInvalid, RegionNotSupported, RedemptionClaimed, RedemptionCooldown
from telegram import Update, ReplyKeyboardRemove, Message, User, WebAppInfo, ReplyKeyboardMarkup, KeyboardButton
from telegram.constants import ChatAction
from telegram import Update, ReplyKeyboardRemove
from telegram.ext import CallbackContext, CommandHandler
from telegram.helpers import escape_markdown
from core.config import config
from core.plugin import handler, Plugin
from core.services.players import PlayersService
from plugins.tools.challenge import ChallengeSystem, ChallengeSystemException
from plugins.tools.genshin import PlayerNotFoundError, CookiesNotFoundError, GenshinHelper
from plugins.tools.sign import SignSystem, NeedChallenge
from utils.log import logger
if TYPE_CHECKING:
from simnet import GenshinClient
class StartPlugin(Plugin):
def __init__(
self,
player: PlayersService,
sign_system: SignSystem,
challenge_system: ChallengeSystem,
genshin_helper: GenshinHelper,
):
self.challenge_system = challenge_system
self.sign_system = sign_system
self.genshin_helper = genshin_helper
self.players_service = player
@handler.command("start", block=False)
async def start(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user
message = update.effective_message
args = context.args
args_text = " ".join(args) if args else ""
logger.info("用户 %s[%s] 发出start命令 args[%s]", user.full_name, user.id, args_text)
if args is not None and len(args) >= 1:
if args[0] == "inline_message":
await message.reply_markdown_v2(
f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 ')}\n"
f"{escape_markdown('发送 /help 命令即可查看命令帮助')}"
)
elif args[0] == "set_cookie":
await message.reply_markdown_v2(
f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 ')}\n"
f"{escape_markdown('发送 /setcookie 命令进入绑定账号流程')}"
)
elif args[0] == "set_uid":
await message.reply_markdown_v2(
f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 ')}\n"
f"{escape_markdown('发送 /setuid 或 /setcookie 命令进入绑定账号流程')}"
)
elif args[0] == "verify_verification":
logger.info("用户 %s[%s] 通过start命令 获取认证信息", user.full_name, user.id)
await self.process_validate(message, user, bot_username=context.bot.username)
elif args[0] == "sign":
logger.info("用户 %s[%s] 通过start命令 获取签到信息", user.full_name, user.id)
await self.get_sign_button(message, user, bot_username=context.bot.username)
elif args[0].startswith("challenge_"):
_data = args[0].split("_")
_command = _data[1]
_challenge = _data[2]
if _command == "sign":
logger.info("用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id)
await self.process_sign_validate(message, user, _challenge)
elif args[0].startswith("redeem_"):
_code = args[0].split("_")[1]
logger.info("用户 %s[%s] 通过start命令 进入兑换码兑换流程 code[%s]", user.full_name, user.id, _code)
await self.process_redeem(message, user, _code)
else:
await message.reply_html(f"你好 {user.mention_html()} !我是派蒙 \n请点击 /{args[0]} 命令进入对应流程")
return
logger.info("用户 %s[%s] 发出start命令", user.full_name, user.id)
await message.reply_markdown_v2(f"你好 {user.mention_markdown_v2()} {escape_markdown('!我是派蒙 ')}")
@staticmethod
@ -90,76 +33,3 @@ class StartPlugin(Plugin):
@handler(CommandHandler, command="reply_keyboard_remove", block=False)
async def reply_keyboard_remove(self, update: Update, _: CallbackContext) -> None:
await update.message.reply_text("移除远程键盘成功", reply_markup=ReplyKeyboardRemove())
async def process_sign_validate(self, message: Message, user: User, validate: str):
try:
async with self.genshin_helper.genshin(user.id) as client:
await message.reply_chat_action(ChatAction.TYPING)
_, challenge = await self.sign_system.get_challenge(client.player_id)
if not challenge:
await message.reply_text("验证请求已过期。", allow_sending_without_reply=True)
return
sign_text = await self.sign_system.start_sign(client, challenge=challenge, validate=validate)
await message.reply_text(sign_text, allow_sending_without_reply=True)
except (PlayerNotFoundError, CookiesNotFoundError):
logger.warning("用户 %s[%s] 账号信息未找到", user.full_name, user.id)
except NeedChallenge:
await message.reply_text("回调错误,请重新签到", allow_sending_without_reply=True)
async def process_validate(self, message: Message, user: User, bot_username: Optional[str] = None):
await message.reply_text(
"由于官方对第三方工具限制以及账户安全的考虑,频繁使用第三方工具会导致账号被风控并要求用过验证才能进行访问。\n"
"如出现频繁验证请求,可尝试使用 /setcookies 的新方法重新绑定以尝试解决该问题。\n"
"如担心账号安全,建议修改密码以保护账号安全,修改账号密码后任何登录令牌都会被强制注销。\n"
)
try:
uid, gt, challenge = await self.challenge_system.create_challenge(user.id, ajax=True)
except ChallengeSystemException as exc:
await message.reply_text(exc.message)
return
if gt == "ajax":
await message.reply_text("验证成功")
return
url = (
f"{config.pass_challenge_user_web}/webapp?"
f"gt={gt}&username={bot_username}&command=verify&challenge={challenge}&uid={uid}"
)
await message.reply_text(
"请尽快在10秒内完成手动验证\n或发送 /web_cancel 取消操作",
reply_markup=ReplyKeyboardMarkup.from_button(
KeyboardButton(
text="点我手动验证",
web_app=WebAppInfo(url=url),
)
),
)
async def get_sign_button(self, message: Message, user: User, bot_username: str):
player = await self.players_service.get_player(user.id)
if player is None:
logger.warning("用户 %s[%s] 账号信息未找到", user.full_name, user.id)
return
await message.reply_chat_action(ChatAction.TYPING)
button = await self.sign_system.get_challenge_button(bot_username, player.player_id, user.id, callback=False)
if not button:
await message.reply_text("验证请求已过期。", allow_sending_without_reply=True)
return
await message.reply_text("请尽快点击下方按钮进行验证。", allow_sending_without_reply=True, reply_markup=button)
async def process_redeem(self, message: Message, user: User, code: str):
try:
if not code:
raise RedemptionInvalid
async with self.genshin_helper.genshin(user.id) as client:
client: "GenshinClient"
await client.redeem_code_by_hoyolab(code)
msg = "兑换码兑换成功。"
except RegionNotSupported:
msg = "此服务器暂不支持进行兑换哦~"
except RedemptionInvalid:
msg = "兑换码格式不正确,请确认。"
except RedemptionClaimed:
msg = "此兑换码已经兑换过了。"
except RedemptionCooldown as e:
msg = e.message
await message.reply_text(msg)

View File

@ -1,6 +1,6 @@
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import CallbackContext
from telegram.ext import CallbackContext, filters
from core.plugin import Plugin, handler
from core.services.template.services import TemplateService
@ -16,6 +16,7 @@ class HelpPlugin(Plugin):
self.template_service = template_service
@handler.command(command="help", block=False)
@handler.command(command="start", filters=filters.Regex("inline_message$"), block=False)
async def start(self, update: Update, _: CallbackContext):
message = update.effective_message
user = update.effective_user

View File

@ -22,20 +22,11 @@ class Redeem(Plugin):
):
self.genshin_helper = genshin_helper
@handler.command(command="redeem", block=False)
@handler.message(filters=filters.Regex("^兑换码兑换(.*)"), block=False)
async def command_start(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user
message = update.effective_message
args = self.get_args(context)
code = args[0] if args else None
logger.info("用户 %s[%s] 兑换码兑换命令请求 code[%s]", user.full_name, user.id, code)
if filters.ChatType.GROUPS.filter(message):
self.add_delete_message_job(message)
async def redeem_code(self, uid: int, code: str) -> str:
try:
if not code:
raise RedemptionInvalid
async with self.genshin_helper.genshin(user.id) as client:
async with self.genshin_helper.genshin(uid) as client:
client: "GenshinClient"
await client.redeem_code_by_hoyolab(code)
msg = "兑换码兑换成功。"
@ -47,6 +38,29 @@ class Redeem(Plugin):
msg = "此兑换码已经兑换过了。"
except RedemptionCooldown as e:
msg = e.message
return msg
@handler.command(command="redeem", block=False)
@handler.message(filters=filters.Regex("^兑换码兑换(.*)"), block=False)
async def command_start(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user
message = update.effective_message
args = self.get_args(context)
code = args[0] if args else None
logger.info("用户 %s[%s] 兑换码兑换命令请求 code[%s]", user.full_name, user.id, code)
if filters.ChatType.GROUPS.filter(message):
self.add_delete_message_job(message)
msg = await self.redeem_code(user.id, code)
reply_message = await message.reply_text(msg)
if filters.ChatType.GROUPS.filter(reply_message):
self.add_delete_message_job(reply_message)
@handler.command(command="start", filters=filters.Regex(r" redeem_(.*)"), block=False)
async def start_redeem(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user
message = update.effective_message
args = self.get_args(context)
code = args[0].split("_")[1]
logger.info("用户 %s[%s] 通过start命令 进入兑换码兑换流程 code[%s]", user.full_name, user.id, code)
msg = await self.redeem_code(user.id, code)
await message.reply_text(msg)

View File

@ -2,8 +2,8 @@ from typing import Optional, Tuple
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import CommandHandler, CallbackContext
from telegram.ext import MessageHandler, filters
from telegram.ext import CallbackContext
from telegram.ext import filters
from telegram.helpers import create_deep_linked_url
from core.config import config
@ -66,8 +66,9 @@ class Sign(Plugin):
await self.sign_service.add(user)
return "开启自动签到成功"
@handler(CommandHandler, command="sign", block=False)
@handler(MessageHandler, filters=filters.Regex("^每日签到(.*)"), block=False)
@handler.command(command="sign", block=False)
@handler.message(filters=filters.Regex("^每日签到(.*)"), block=False)
@handler.command(command="start", filters=filters.Regex("sign$"), block=False)
async def command_start(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user
message = update.effective_message
@ -127,6 +128,26 @@ class Sign(Plugin):
if filters.ChatType.GROUPS.filter(reply_message):
self.add_delete_message_job(reply_message)
@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
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:
async with self.genshin_helper.genshin(user.id) as client:
await message.reply_chat_action(ChatAction.TYPING)
_, challenge = await self.sign_system.get_challenge(client.player_id)
if not challenge:
await message.reply_text("验证请求已过期。", allow_sending_without_reply=True)
return
sign_text = await self.sign_system.start_sign(client, challenge=challenge, validate=validate)
await message.reply_text(sign_text, allow_sending_without_reply=True)
except NeedChallenge:
await message.reply_text("回调错误,请重新签到", allow_sending_without_reply=True)
@handler(CallbackQueryHandler, pattern=r"^sign\|", block=False)
async def sign_gen_link(self, update: Update, context: CallbackContext) -> None:
callback_query = update.callback_query

View File

@ -15,13 +15,14 @@ class VerificationPlugins(Plugin):
self.challenge_system = challenge_system
@handler.command("verify", filters=filters.ChatType.PRIVATE, block=False)
@handler.command(command="start", filters=filters.Regex("verify_verification$"), block=False)
async def verify(self, update: Update, context: CallbackContext) -> None:
user = update.effective_user
message = update.effective_message
logger.info("用户 %s[%s] 发出verify命令", user.full_name, user.id)
try:
uid, gt, challenge = await self.challenge_system.create_challenge(
user.id, context.args is not None and not len(context.args) >= 1
user.id, context.args is not None and len(context.args) < 1
)
except ChallengeSystemException as exc:
await message.reply_text(exc.message)