2022-10-11 12:34:54 +00:00
|
|
|
|
import contextlib
|
2022-12-01 11:54:40 +00:00
|
|
|
|
from typing import Dict, Optional
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
|
|
|
|
import genshin
|
2022-12-08 14:22:59 +00:00
|
|
|
|
from arkowrapper import ArkoWrapper
|
2022-12-01 11:54:40 +00:00
|
|
|
|
from genshin import DataNotPublic, GenshinException, InvalidCookies, types
|
2022-11-19 13:41:12 +00:00
|
|
|
|
from genshin.models import GenshinAccount
|
2022-12-01 11:54:40 +00:00
|
|
|
|
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, TelegramObject, Update
|
2022-12-23 13:06:08 +00:00
|
|
|
|
from telegram.constants import ParseMode
|
2022-12-01 11:54:40 +00:00
|
|
|
|
from telegram.ext import CallbackContext, ConversationHandler, filters
|
2022-08-05 13:23:04 +00:00
|
|
|
|
from telegram.helpers import escape_markdown
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.baseplugin import BasePlugin
|
|
|
|
|
from core.cookies.error import CookiesNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.cookies.services import CookiesService
|
2022-12-01 11:54:40 +00:00
|
|
|
|
from core.plugin import Plugin, conversation, handler
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.user.error import UserNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.user.models import User
|
|
|
|
|
from core.user.services import UserService
|
2023-02-21 09:43:40 +00:00
|
|
|
|
from modules.apihelper.client.components.authclient import AuthClient
|
2023-02-22 04:47:44 +00:00
|
|
|
|
from modules.apihelper.models.genshin.cookies import CookiesModel
|
2022-08-05 13:23:04 +00:00
|
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
|
|
|
|
from utils.models.base import RegionEnum
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AddUserCommandData(TelegramObject):
|
|
|
|
|
user: Optional[User] = None
|
2022-09-09 11:30:02 +00:00
|
|
|
|
region: RegionEnum = RegionEnum.NULL
|
2022-08-05 13:23:04 +00:00
|
|
|
|
cookies: dict = {}
|
|
|
|
|
game_uid: int = 0
|
2022-09-17 14:58:54 +00:00
|
|
|
|
phone: int = 0
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
|
|
|
|
|
2022-12-23 13:06:08 +00:00
|
|
|
|
CHECK_SERVER, INPUT_COOKIES, COMMAND_RESULT = range(10100, 10103)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
2022-09-08 01:49:44 +00:00
|
|
|
|
class SetUserCookies(Plugin.Conversation, BasePlugin.Conversation):
|
|
|
|
|
"""Cookie绑定"""
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, user_service: UserService = None, cookies_service: CookiesService = None):
|
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
|
self.user_service = user_service
|
|
|
|
|
|
2022-12-08 14:22:59 +00:00
|
|
|
|
# noinspection SpellCheckingInspection
|
2022-11-27 10:42:49 +00:00
|
|
|
|
@staticmethod
|
2022-12-08 14:22:59 +00:00
|
|
|
|
def parse_cookie(cookie: Dict[str, str]) -> Dict[str, str]:
|
2022-11-27 10:42:49 +00:00
|
|
|
|
cookies = {}
|
2022-12-08 14:22:59 +00:00
|
|
|
|
|
2023-02-21 14:34:53 +00:00
|
|
|
|
v1_keys = ["ltoken", "ltuid", "account_id", "cookie_token", "stoken", "stuid", "login_uid", "login_ticket"]
|
|
|
|
|
v2_keys = ["ltoken_v2", "ltmid_v2", "ltuid_v2", "account_mid_v2", "cookie_token_v2", "account_id_v2"]
|
2022-12-08 14:22:59 +00:00
|
|
|
|
|
|
|
|
|
for k in v1_keys + v2_keys:
|
|
|
|
|
cookies[k] = cookie.get(k)
|
|
|
|
|
|
|
|
|
|
return {k: v for k, v in cookies.items() if v is not None}
|
2022-11-27 10:42:49 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@conversation.entry_point
|
2022-10-12 09:35:59 +00:00
|
|
|
|
@handler.command(command="setcookie", filters=filters.ChatType.PRIVATE, block=True)
|
2022-11-29 13:15:34 +00:00
|
|
|
|
@handler.command(command="setcookies", filters=filters.ChatType.PRIVATE, block=True)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
@restricts()
|
|
|
|
|
@error_callable
|
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 绑定账号命令请求", user.full_name, user.id)
|
2023-01-27 03:50:47 +00:00
|
|
|
|
cookies_command_data = AddUserCommandData()
|
|
|
|
|
context.chat_data["add_user_command_data"] = cookies_command_data
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
text = f'你好 {user.mention_markdown_v2()} {escape_markdown("!请选择要绑定的服务器!或回复退出取消操作")}'
|
2022-08-05 13:23:04 +00:00
|
|
|
|
reply_keyboard = [["米游社", "HoYoLab"], ["退出"]]
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
|
|
|
|
|
return CHECK_SERVER
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
2022-09-17 14:58:54 +00:00
|
|
|
|
@conversation.entry_point
|
2023-02-02 10:40:13 +00:00
|
|
|
|
@handler.command("qlogin", filters=filters.ChatType.PRIVATE, block=False)
|
2022-09-17 14:58:54 +00:00
|
|
|
|
@error_callable
|
2022-12-23 13:06:08 +00:00
|
|
|
|
async def qrcode_login(self, update: Update, context: CallbackContext):
|
2022-09-17 14:58:54 +00:00
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
2022-12-23 13:06:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 绑定账号命令请求", user.full_name, user.id)
|
2023-01-27 03:50:47 +00:00
|
|
|
|
add_user_command_data = AddUserCommandData()
|
|
|
|
|
context.chat_data["add_user_command_data"] = add_user_command_data
|
2022-12-27 06:30:36 +00:00
|
|
|
|
add_user_command_data.region = RegionEnum.HYPERION
|
2022-12-23 13:06:08 +00:00
|
|
|
|
try:
|
|
|
|
|
user_info = await self.user_service.get_user_by_id(user.id)
|
|
|
|
|
except UserNotFoundError:
|
|
|
|
|
user_info = None
|
|
|
|
|
if user_info is not None:
|
|
|
|
|
try:
|
2023-01-27 03:50:47 +00:00
|
|
|
|
await self.cookies_service.get_cookies(user.id, RegionEnum.HYPERION)
|
2022-12-23 13:06:08 +00:00
|
|
|
|
except CookiesNotFoundError:
|
|
|
|
|
await message.reply_text("你已经绑定UID,如果继续操作会覆盖当前UID。")
|
|
|
|
|
else:
|
|
|
|
|
await message.reply_text("警告,你已经绑定Cookie,如果继续操作会覆盖当前Cookie。")
|
|
|
|
|
add_user_command_data.user = user_info
|
2023-02-21 09:43:40 +00:00
|
|
|
|
auth_client = AuthClient()
|
|
|
|
|
url, ticket = await auth_client.create_qrcode_login()
|
|
|
|
|
data = auth_client.generate_qrcode(url)
|
2022-12-23 13:06:08 +00:00
|
|
|
|
text = f"你好 {user.mention_html()} !该绑定方法仅支持国服,请在3分钟内使用米游社扫码并确认进行绑定。"
|
|
|
|
|
await message.reply_photo(data, caption=text, parse_mode=ParseMode.HTML)
|
2023-02-21 09:43:40 +00:00
|
|
|
|
if await auth_client.check_qrcode_login(ticket):
|
|
|
|
|
add_user_command_data.cookies = auth_client.cookies.to_dict()
|
2022-12-23 13:06:08 +00:00
|
|
|
|
return await self.check_cookies(update, context)
|
|
|
|
|
else:
|
|
|
|
|
await message.reply_markdown_v2("可能是验证码已过期或者你没有同意授权,请重新发送命令进行绑定。")
|
|
|
|
|
return ConversationHandler.END
|
2022-09-17 14:58:54 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@conversation.state(state=CHECK_SERVER)
|
|
|
|
|
@handler.message(filters=filters.TEXT & ~filters.COMMAND, block=True)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
@error_callable
|
|
|
|
|
async def check_server(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-08-05 13:23:04 +00:00
|
|
|
|
add_user_command_data: AddUserCommandData = context.chat_data.get("add_user_command_data")
|
2022-09-08 01:08:37 +00:00
|
|
|
|
if message.text == "退出":
|
|
|
|
|
await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-09-08 01:08:37 +00:00
|
|
|
|
elif message.text == "米游社":
|
|
|
|
|
region = RegionEnum.HYPERION
|
2022-12-24 06:18:27 +00:00
|
|
|
|
bbs_url = "https://user.mihoyo.com/"
|
2022-08-05 13:23:04 +00:00
|
|
|
|
bbs_name = "米游社"
|
2022-09-08 01:08:37 +00:00
|
|
|
|
elif message.text == "HoYoLab":
|
2022-08-05 13:23:04 +00:00
|
|
|
|
bbs_url = "https://www.hoyolab.com/home"
|
|
|
|
|
bbs_name = "HoYoLab"
|
2022-09-08 01:08:37 +00:00
|
|
|
|
region = RegionEnum.HOYOLAB
|
2022-08-05 13:23:04 +00:00
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("选择错误,请重新选择")
|
|
|
|
|
return CHECK_SERVER
|
|
|
|
|
try:
|
|
|
|
|
user_info = await self.user_service.get_user_by_id(user.id)
|
|
|
|
|
except UserNotFoundError:
|
|
|
|
|
user_info = None
|
|
|
|
|
if user_info is not None:
|
|
|
|
|
try:
|
2023-01-27 03:50:47 +00:00
|
|
|
|
await self.cookies_service.get_cookies(user.id, region)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
except CookiesNotFoundError:
|
|
|
|
|
await message.reply_text("你已经绑定UID,如果继续操作会覆盖当前UID。")
|
|
|
|
|
else:
|
|
|
|
|
await message.reply_text("警告,你已经绑定Cookie,如果继续操作会覆盖当前Cookie。")
|
|
|
|
|
add_user_command_data.user = user_info
|
|
|
|
|
add_user_command_data.region = region
|
|
|
|
|
await message.reply_text(f"请输入{bbs_name}的Cookies!或回复退出取消操作", reply_markup=ReplyKeyboardRemove())
|
2022-11-26 08:33:55 +00:00
|
|
|
|
if bbs_name == "米游社":
|
|
|
|
|
help_message = (
|
|
|
|
|
"<b>关于如何获取Cookies</b>\n"
|
|
|
|
|
"<b>现在因为网站HttpOnly策略无法通过脚本获取,因此操作只能在PC上运行。</b>\n\n"
|
|
|
|
|
"PC:\n"
|
2023-02-08 09:07:11 +00:00
|
|
|
|
"1、<a href='https://user.mihoyo.com/'>打开通行证并登录</a>\n"
|
|
|
|
|
"2、进入通行证按F12打开开发者工具\n"
|
|
|
|
|
"3、将开发者工具切换至网络(Network)并点击过滤栏中的文档(Document)并刷新页面\n"
|
|
|
|
|
"4、在请求列表中选择第一个并点击\n"
|
|
|
|
|
"5、找到并复制请求标头(Request Headers)中的<b>Cookie</b>\n"
|
2022-11-26 08:33:55 +00:00
|
|
|
|
"<u>如发现没有请求标头(Request Headers)大概因为缓存的存在需要你点击禁用缓存(Disable Cache)再次刷新页面</u>"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
javascript = (
|
|
|
|
|
"javascript:(()=>{_=(n)=>{for(i in(r=document.cookie.split(';'))){var a=r[i].split('=');if(a["
|
|
|
|
|
"0].trim()==n)return a[1]}};c=_('account_id')||alert('无效的Cookie,请重新登录!');c&&confirm("
|
|
|
|
|
"'将Cookie复制到剪贴板?')&©(document.cookie)})(); "
|
|
|
|
|
)
|
|
|
|
|
javascript_android = "javascript:(()=>{prompt('',document.cookie)})();"
|
|
|
|
|
help_message = (
|
|
|
|
|
f"<b>关于如何获取Cookies</b>\n\n"
|
|
|
|
|
f"PC:\n"
|
|
|
|
|
f"1、<a href='{bbs_url}'>打开 {bbs_name} 并登录</a>\n"
|
|
|
|
|
"2、按F12打开开发者工具\n"
|
|
|
|
|
"3、将开发者工具切换至控制台(Console)\n"
|
|
|
|
|
"4、复制下方的代码,并将其粘贴在控制台中,按下回车\n"
|
2022-12-30 09:01:54 +00:00
|
|
|
|
f"<pre><code class='javascript'>{javascript}</code></pre>\n"
|
2022-11-26 08:33:55 +00:00
|
|
|
|
"Android:\n"
|
|
|
|
|
f"1、<a href='{bbs_url}'>通过 Via 打开 {bbs_name} 并登录</a>\n"
|
|
|
|
|
"2、复制下方的代码,并将其粘贴在地址栏中,点击右侧箭头\n"
|
|
|
|
|
f"<code>{javascript_android}</code>\n"
|
|
|
|
|
"iOS:\n"
|
|
|
|
|
"1、在App Store上安装Web Inspector,并在iOS设置- Safari浏览器-扩展-允许这些扩展下找到Web Inspector-打开,允许所有网站\n"
|
|
|
|
|
f"2、<a href='{bbs_url}'>通过 Safari 打开 {bbs_name} 并登录</a>\n"
|
2022-12-30 09:01:54 +00:00
|
|
|
|
"3、点击地址栏左侧的大小按钮 - Web Inspector扩展 - Console - 点击下方文本框复制下方代码粘贴:\n"
|
|
|
|
|
f"<pre><code class='javascript'>{javascript}</code></pre>\n"
|
2022-11-26 08:33:55 +00:00
|
|
|
|
"4、点击Console下的Execute"
|
|
|
|
|
)
|
2022-11-23 00:59:25 +00:00
|
|
|
|
await message.reply_html(help_message, disable_web_page_preview=True)
|
2022-09-17 14:58:54 +00:00
|
|
|
|
return INPUT_COOKIES
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
2022-09-17 14:58:54 +00:00
|
|
|
|
@conversation.state(state=INPUT_COOKIES)
|
|
|
|
|
@handler.message(filters=filters.TEXT & ~filters.COMMAND, block=True)
|
|
|
|
|
@error_callable
|
|
|
|
|
async def input_cookies(self, update: Update, context: CallbackContext) -> int:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-11-27 10:42:49 +00:00
|
|
|
|
user = update.effective_user
|
2022-08-05 13:23:04 +00:00
|
|
|
|
add_user_command_data: AddUserCommandData = context.chat_data.get("add_user_command_data")
|
2022-09-08 01:08:37 +00:00
|
|
|
|
if message.text == "退出":
|
|
|
|
|
await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-11-27 10:42:49 +00:00
|
|
|
|
try:
|
2022-12-10 12:24:40 +00:00
|
|
|
|
# cookie str to dict
|
|
|
|
|
wrapped = (
|
|
|
|
|
ArkoWrapper(message.text.split(";"))
|
2022-12-11 06:00:12 +00:00
|
|
|
|
.filter(lambda x: x != "")
|
2022-12-10 12:24:40 +00:00
|
|
|
|
.map(lambda x: x.strip())
|
2023-02-21 14:19:28 +00:00
|
|
|
|
.map(lambda x: ((y := x.split("=", 1))[0], y[1]))
|
2022-12-10 12:24:40 +00:00
|
|
|
|
)
|
|
|
|
|
cookie = {x[0]: x[1] for x in wrapped}
|
2022-12-01 10:41:27 +00:00
|
|
|
|
cookies = self.parse_cookie(cookie)
|
2022-12-11 06:00:12 +00:00
|
|
|
|
except (AttributeError, ValueError, IndexError) as exc:
|
2022-12-10 12:24:40 +00:00
|
|
|
|
logger.info("用户 %s[%s] Cookies解析出现错误\ntext:%s", user.full_name, user.id, message.text)
|
2022-11-27 10:42:49 +00:00
|
|
|
|
logger.debug("解析Cookies出现错误", exc_info=exc)
|
|
|
|
|
await message.reply_text("解析Cookies出现错误,请检查是否正确", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-08-06 06:22:37 +00:00
|
|
|
|
if not cookies:
|
2022-11-27 10:42:49 +00:00
|
|
|
|
logger.info("用户 %s[%s] Cookies格式有误", user.full_name, user.id)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("Cookies格式有误,请检查", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-09-17 14:58:54 +00:00
|
|
|
|
add_user_command_data.cookies = cookies
|
|
|
|
|
return await self.check_cookies(update, context)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def check_cookies(update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
|
|
|
|
add_user_command_data: AddUserCommandData = context.chat_data.get("add_user_command_data")
|
2023-02-22 04:47:44 +00:00
|
|
|
|
cookies = CookiesModel(**add_user_command_data.cookies)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
if add_user_command_data.region == RegionEnum.HYPERION:
|
2023-02-22 04:47:44 +00:00
|
|
|
|
client = genshin.Client(cookies=cookies.to_dict(), region=types.Region.CHINESE)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
elif add_user_command_data.region == RegionEnum.HOYOLAB:
|
2023-02-22 04:47:44 +00:00
|
|
|
|
client = genshin.Client(cookies=cookies.to_dict(), region=types.Region.OVERSEAS)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
else:
|
2022-11-27 10:42:49 +00:00
|
|
|
|
logger.error("用户 %s[%s] region 异常", user.full_name, user.id)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("数据错误", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2023-02-22 04:47:44 +00:00
|
|
|
|
if not cookies.check():
|
|
|
|
|
await message.reply_text("检测到Cookie不完整,可能会出现问题。", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
try:
|
2023-02-22 04:47:44 +00:00
|
|
|
|
if client.cookie_manager.user_id is None:
|
|
|
|
|
if cookies.is_v2:
|
|
|
|
|
logger.info("检测到用户 %s[%s] 使用 V2 Cookie 正在尝试获取 account_id", user.full_name, user.id)
|
|
|
|
|
if client.region == types.Region.CHINESE:
|
|
|
|
|
account_info = await client.get_hoyolab_user()
|
|
|
|
|
account_id = account_info.hoyolab_id
|
|
|
|
|
cookies.set_v2_uid(account_id)
|
|
|
|
|
logger.success("获取用户 %s[%s] account_id[%s] 成功", user.full_name, user.id, account_id)
|
|
|
|
|
else:
|
|
|
|
|
logger.warning("用户 %s[%s] region[%s] 也许是不正确的", user.full_name, user.id, client.region.name)
|
2022-11-19 13:41:12 +00:00
|
|
|
|
genshin_accounts = await client.genshin_accounts()
|
2022-08-05 13:23:04 +00:00
|
|
|
|
except DataNotPublic:
|
2022-11-27 10:42:49 +00:00
|
|
|
|
logger.info("用户 %s[%s] 账号疑似被注销", user.full_name, user.id)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("账号疑似被注销,请检查账号状态", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except InvalidCookies:
|
2022-11-27 10:42:49 +00:00
|
|
|
|
logger.info("用户 %s[%s] Cookies已经过期", user.full_name, user.id)
|
2022-12-22 15:27:26 +00:00
|
|
|
|
await message.reply_text(
|
2022-12-24 06:18:27 +00:00
|
|
|
|
"获取账号信息失败,返回Cookies已经过期,请尝试在无痕浏览器中登录获取Cookies。", reply_markup=ReplyKeyboardRemove()
|
2022-12-22 15:27:26 +00:00
|
|
|
|
)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-10-07 12:23:25 +00:00
|
|
|
|
except GenshinException as exc:
|
2022-11-27 10:42:49 +00:00
|
|
|
|
logger.info("用户 %s[%s] 获取账号信息发生错误 [%s]%s", user.full_name, user.id, exc.retcode, exc.original)
|
2022-10-07 12:23:25 +00:00
|
|
|
|
await message.reply_text(
|
2022-11-27 10:42:49 +00:00
|
|
|
|
f"获取账号信息发生错误,错误信息为 {exc.original},请检查Cookie或者账号是否正常", reply_markup=ReplyKeyboardRemove()
|
2022-09-08 01:08:37 +00:00
|
|
|
|
)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-11-27 10:42:49 +00:00
|
|
|
|
except (AttributeError, ValueError) as exc:
|
|
|
|
|
logger.warning("用户 %s[%s] Cookies错误", user.full_name, user.id)
|
2022-12-23 13:06:08 +00:00
|
|
|
|
logger.debug("用户 %s[%s] Cookies错误", user.full_name, user.id, exc_info=exc)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("Cookies错误,请检查是否正确", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-10-11 12:34:54 +00:00
|
|
|
|
with contextlib.suppress(Exception):
|
2023-02-22 04:47:44 +00:00
|
|
|
|
if cookies.login_ticket is not None:
|
|
|
|
|
auth_client = AuthClient(cookies=cookies)
|
2023-02-21 09:43:40 +00:00
|
|
|
|
if await auth_client.get_stoken_by_login_ticket():
|
|
|
|
|
logger.success("用户 %s[%s] 绑定时获取 stoken 成功", user.full_name, user.id)
|
|
|
|
|
if await auth_client.get_cookie_token_by_stoken():
|
|
|
|
|
logger.success("用户 %s[%s] 绑定时获取 cookie_token 成功", user.full_name, user.id)
|
|
|
|
|
if await auth_client.get_ltoken_by_stoken():
|
|
|
|
|
logger.success("用户 %s[%s] 绑定时获取 ltoken 成功", user.full_name, user.id)
|
|
|
|
|
auth_client.cookies.remove_v2()
|
2022-11-19 13:41:12 +00:00
|
|
|
|
user_info: Optional[GenshinAccount] = None
|
|
|
|
|
level: int = 0
|
|
|
|
|
# todo : 多账号绑定
|
|
|
|
|
for genshin_account in genshin_accounts:
|
|
|
|
|
if genshin_account.level >= level: # 获取账号等级最高的
|
|
|
|
|
level = genshin_account.level
|
|
|
|
|
user_info = genshin_account
|
|
|
|
|
if user_info is None:
|
|
|
|
|
await message.reply_text("未找到原神账号,请确认账号信息无误。")
|
|
|
|
|
return ConversationHandler.END
|
2022-08-05 13:23:04 +00:00
|
|
|
|
add_user_command_data.game_uid = user_info.uid
|
|
|
|
|
reply_keyboard = [["确认", "退出"]]
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("获取角色基础信息成功,请检查是否正确!")
|
2022-12-30 09:01:54 +00:00
|
|
|
|
logger.info("用户 %s[%s] 获取账号 %s[%s] 信息成功", user.full_name, user.id, user_info.nickname, user_info.uid)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
text = (
|
|
|
|
|
f"*角色信息*\n"
|
|
|
|
|
f"角色名称:{escape_markdown(user_info.nickname, version=2)}\n"
|
|
|
|
|
f"角色等级:{user_info.level}\n"
|
|
|
|
|
f"UID:`{user_info.uid}`\n"
|
|
|
|
|
f"服务器名称:`{user_info.server_name}`\n"
|
2022-10-10 11:07:28 +00:00
|
|
|
|
)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
|
2023-02-22 11:42:32 +00:00
|
|
|
|
add_user_command_data.cookies = cookies.to_dict()
|
2022-09-08 01:08:37 +00:00
|
|
|
|
return COMMAND_RESULT
|
2022-08-05 13:23:04 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@conversation.state(state=COMMAND_RESULT)
|
|
|
|
|
@handler.message(filters=filters.TEXT & ~filters.COMMAND, block=True)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
@error_callable
|
|
|
|
|
async def command_result(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-08-05 13:23:04 +00:00
|
|
|
|
add_user_command_data: AddUserCommandData = context.chat_data.get("add_user_command_data")
|
2022-09-08 01:08:37 +00:00
|
|
|
|
if message.text == "退出":
|
|
|
|
|
await message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-09-08 01:08:37 +00:00
|
|
|
|
elif message.text == "确认":
|
2022-08-05 13:23:04 +00:00
|
|
|
|
if add_user_command_data.user is None:
|
|
|
|
|
if add_user_command_data.region == RegionEnum.HYPERION:
|
2022-08-05 14:45:54 +00:00
|
|
|
|
user_db = User(
|
|
|
|
|
user_id=user.id,
|
|
|
|
|
yuanshen_uid=add_user_command_data.game_uid,
|
|
|
|
|
region=add_user_command_data.region,
|
|
|
|
|
)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
elif add_user_command_data.region == RegionEnum.HOYOLAB:
|
2022-08-05 14:45:54 +00:00
|
|
|
|
user_db = User(
|
|
|
|
|
user_id=user.id, genshin_uid=add_user_command_data.game_uid, region=add_user_command_data.region
|
|
|
|
|
)
|
2022-08-05 13:23:04 +00:00
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("数据错误")
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
await self.user_service.add_user(user_db)
|
|
|
|
|
else:
|
|
|
|
|
user_db = add_user_command_data.user
|
|
|
|
|
user_db.region = add_user_command_data.region
|
|
|
|
|
if add_user_command_data.region == RegionEnum.HYPERION:
|
|
|
|
|
user_db.yuanshen_uid = add_user_command_data.game_uid
|
|
|
|
|
elif add_user_command_data.region == RegionEnum.HOYOLAB:
|
|
|
|
|
user_db.genshin_uid = add_user_command_data.game_uid
|
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("数据错误")
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
await self.user_service.update_user(user_db)
|
2023-01-27 03:50:47 +00:00
|
|
|
|
await self.cookies_service.add_or_update_cookies(
|
|
|
|
|
user.id, add_user_command_data.cookies, add_user_command_data.region
|
|
|
|
|
)
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 绑定账号成功", user.full_name, user.id)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("保存成功", reply_markup=ReplyKeyboardRemove())
|
2022-08-05 13:23:04 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await message.reply_text("回复错误,请重新输入")
|
|
|
|
|
return COMMAND_RESULT
|