Support hoyolab redeem code

This commit is contained in:
xtaodada 2023-09-15 23:47:57 +08:00
parent 0810c5bd2c
commit dff039ad7a
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 75 additions and 1 deletions

View File

@ -1,5 +1,6 @@
from typing import Optional
from typing import Optional, TYPE_CHECKING
from simnet.errors import RedemptionInvalid, RegionNotSupported, RedemptionClaimed
from telegram import Update, ReplyKeyboardRemove, Message, User, WebAppInfo, ReplyKeyboardMarkup, KeyboardButton
from telegram.constants import ChatAction
from telegram.ext import CallbackContext, CommandHandler
@ -13,6 +14,9 @@ from plugins.tools.genshin import PlayerNotFoundError, CookiesNotFoundError, Gen
from plugins.tools.sign import SignSystem, NeedChallenge
from utils.log import logger
if TYPE_CHECKING:
from simnet import StarRailClient
class StartPlugin(Plugin):
def __init__(
@ -61,6 +65,10 @@ class StartPlugin(Plugin):
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
@ -133,3 +141,19 @@ class StartPlugin(Plugin):
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: "StarRailClient"
await client.redeem_code_by_hoyolab(code)
msg = "兑换码兑换成功。"
except RegionNotSupported:
msg = "此服务器暂不支持进行兑换哦~"
except RedemptionInvalid:
msg = "兑换码格式不正确,请确认。"
except RedemptionClaimed:
msg = "此兑换码已经兑换过了。"
await message.reply_text(msg)

View File

@ -0,0 +1,50 @@
from typing import TYPE_CHECKING
from simnet.errors import RedemptionInvalid, RedemptionClaimed, RegionNotSupported
from telegram import Update
from telegram.ext import CallbackContext
from telegram.ext import filters
from core.plugin import Plugin, handler
from plugins.tools.genshin import GenshinHelper
from utils.log import logger
if TYPE_CHECKING:
from simnet import StarRailClient
class Redeem(Plugin):
"""兑换码兑换"""
def __init__(
self,
genshin_helper: GenshinHelper,
):
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)
try:
if not code:
raise RedemptionInvalid
async with self.genshin_helper.genshin(user.id) as client:
client: "StarRailClient"
await client.redeem_code_by_hoyolab(code)
msg = "兑换码兑换成功。"
except RegionNotSupported:
msg = "此服务器暂不支持进行兑换哦~"
except RedemptionInvalid:
msg = "兑换码格式不正确,请确认。"
except RedemptionClaimed:
msg = "此兑换码已经兑换过了。"
reply_message = await message.reply_text(msg)
if filters.ChatType.GROUPS.filter(reply_message):
self.add_delete_message_job(reply_message)