Support hoyolab redeem code

This commit is contained in:
omg-xtao 2023-09-15 23:56:22 +08:00 committed by GitHub
parent c2bcd0bf19
commit f7f1bd1a62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 import Update, ReplyKeyboardRemove, Message, User, WebAppInfo, ReplyKeyboardMarkup, KeyboardButton
from telegram.constants import ChatAction from telegram.constants import ChatAction
from telegram.ext import CallbackContext, CommandHandler 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 plugins.tools.sign import SignSystem, NeedChallenge
from utils.log import logger from utils.log import logger
if TYPE_CHECKING:
from simnet import GenshinClient
class StartPlugin(Plugin): class StartPlugin(Plugin):
def __init__( def __init__(
@ -61,6 +65,10 @@ class StartPlugin(Plugin):
if _command == "sign": if _command == "sign":
logger.info("用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id) logger.info("用户 %s[%s] 通过start命令 进入签到流程", user.full_name, user.id)
await self.process_sign_validate(message, user, _challenge) 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: else:
await message.reply_html(f"你好 {user.mention_html()} !我是派蒙 \n请点击 /{args[0]} 命令进入对应流程") await message.reply_html(f"你好 {user.mention_html()} !我是派蒙 \n请点击 /{args[0]} 命令进入对应流程")
return return
@ -137,3 +145,19 @@ class StartPlugin(Plugin):
await message.reply_text("验证请求已过期。", allow_sending_without_reply=True) await message.reply_text("验证请求已过期。", allow_sending_without_reply=True)
return return
await message.reply_text("请尽快点击下方按钮进行验证。", allow_sending_without_reply=True, reply_markup=button) 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 = "此兑换码已经兑换过了。"
await message.reply_text(msg)

50
plugins/genshin/redeem.py Normal file
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 GenshinClient
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: "GenshinClient"
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)