MibooGram/plugins/genshin/redeem.py

67 lines
2.6 KiB
Python
Raw Normal View History

2023-09-15 15:56:22 +00:00
from typing import TYPE_CHECKING
2023-09-30 07:27:28 +00:00
from simnet.errors import RedemptionInvalid, RedemptionClaimed, RegionNotSupported, RedemptionCooldown
2023-09-15 15:56:22 +00:00
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
2023-12-24 13:26:36 +00:00
async def redeem_code(self, uid: int, code: str) -> str:
2023-09-15 15:56:22 +00:00
try:
if not code:
raise RedemptionInvalid
2023-12-24 13:26:36 +00:00
async with self.genshin_helper.genshin(uid) as client:
2023-09-15 15:56:22 +00:00
client: "GenshinClient"
await client.redeem_code_by_hoyolab(code)
msg = "兑换码兑换成功。"
except RegionNotSupported:
msg = "此服务器暂不支持进行兑换哦~"
except RedemptionInvalid:
msg = "兑换码格式不正确,请确认。"
except RedemptionClaimed:
msg = "此兑换码已经兑换过了。"
2023-09-30 07:27:28 +00:00
except RedemptionCooldown as e:
msg = e.message
2023-12-24 13:26:36 +00:00
return msg
@handler.command(command="redeem", cookie=True, block=False)
@handler.message(filters=filters.Regex("^兑换码兑换(.*)"), cookie=True, block=False)
2023-12-24 13:26:36 +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)
2023-12-24 13:26:36 +00:00
message = update.effective_message
args = self.get_args(context)
code = args[0] if args else None
2024-03-10 12:40:26 +00:00
self.log_user(update, logger.info, "兑换码兑换命令请求 code[%s]", code)
2023-12-24 13:26:36 +00:00
if filters.ChatType.GROUPS.filter(message):
self.add_delete_message_job(message)
2024-03-10 12:40:26 +00:00
msg = await self.redeem_code(user_id, code)
2023-09-15 15:56:22 +00:00
reply_message = await message.reply_text(msg)
if filters.ChatType.GROUPS.filter(reply_message):
self.add_delete_message_job(reply_message)
2023-12-24 13:26:36 +00:00
@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)