2022-06-21 09:58:35 +00:00
|
|
|
|
from http.cookies import SimpleCookie, CookieError
|
2022-06-09 07:12:06 +00:00
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
import genshin
|
2022-06-09 07:12:06 +00:00
|
|
|
|
import ujson
|
2022-08-09 15:36:12 +00:00
|
|
|
|
from genshin import InvalidCookies, GenshinException, DataNotPublic, types
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from telegram import Update, ReplyKeyboardRemove, ReplyKeyboardMarkup
|
2022-06-09 05:26:08 +00:00
|
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters, ConversationHandler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from telegram.helpers import escape_markdown
|
|
|
|
|
|
2022-05-18 11:18:21 +00:00
|
|
|
|
from logger import Log
|
2022-06-26 06:17:43 +00:00
|
|
|
|
from manager import listener_plugins_class
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from model.base import ServiceEnum
|
2022-07-07 01:36:34 +00:00
|
|
|
|
from plugins.base import restricts, BasePlugins
|
2022-06-03 07:54:56 +00:00
|
|
|
|
from plugins.errorhandler import conversation_error_handler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from service.base import UserInfoData
|
2022-07-07 01:36:34 +00:00
|
|
|
|
from utils.base import PaimonContext
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CookiesCommandData:
|
2022-06-19 11:37:31 +00:00
|
|
|
|
service = ServiceEnum.HYPERION
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies: dict = {}
|
|
|
|
|
game_uid: int = 0
|
|
|
|
|
user_info: UserInfoData = UserInfoData()
|
|
|
|
|
|
|
|
|
|
|
2022-06-26 06:17:43 +00:00
|
|
|
|
@listener_plugins_class()
|
2022-04-14 07:18:45 +00:00
|
|
|
|
class Cookies(BasePlugins):
|
2022-06-09 12:52:59 +00:00
|
|
|
|
"""
|
|
|
|
|
Cookie绑定
|
|
|
|
|
"""
|
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
CHECK_SERVER, CHECK_COOKIES, COMMAND_RESULT = range(10100, 10103)
|
|
|
|
|
|
2022-07-07 01:36:34 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def create_handlers(cls):
|
|
|
|
|
cookies = cls()
|
2022-06-09 05:26:08 +00:00
|
|
|
|
cookies_handler = ConversationHandler(
|
|
|
|
|
entry_points=[CommandHandler('adduser', cookies.command_start, filters.ChatType.PRIVATE, block=True),
|
2022-06-09 07:12:06 +00:00
|
|
|
|
MessageHandler(filters.Regex(r"^绑定账号(.*)") & filters.ChatType.PRIVATE,
|
|
|
|
|
cookies.command_start, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
states={
|
|
|
|
|
cookies.CHECK_SERVER: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
2022-06-09 07:12:06 +00:00
|
|
|
|
cookies.check_server, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
cookies.CHECK_COOKIES: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
2022-06-09 07:12:06 +00:00
|
|
|
|
cookies.check_cookies, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
cookies.COMMAND_RESULT: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
|
|
|
|
cookies.command_result, block=True)],
|
|
|
|
|
},
|
|
|
|
|
fallbacks=[CommandHandler('cancel', cookies.cancel, block=True)],
|
|
|
|
|
)
|
2022-06-26 06:17:43 +00:00
|
|
|
|
return [cookies_handler]
|
2022-06-09 05:26:08 +00:00
|
|
|
|
|
2022-06-18 11:33:57 +00:00
|
|
|
|
@conversation_error_handler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> int:
|
2022-05-18 11:18:21 +00:00
|
|
|
|
user = update.effective_user
|
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 绑定账号命令请求")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies_command_data: CookiesCommandData = context.chat_data.get("cookies_command_data")
|
|
|
|
|
if cookies_command_data is None:
|
|
|
|
|
cookies_command_data = CookiesCommandData()
|
|
|
|
|
context.chat_data["cookies_command_data"] = cookies_command_data
|
2022-05-18 11:18:21 +00:00
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
message = f'你好 {user.mention_markdown_v2()} {escape_markdown("!请选择要绑定的服务器!或回复退出取消操作")}'
|
2022-04-30 06:51:24 +00:00
|
|
|
|
reply_keyboard = [['米游社', 'HoYoLab'], ["退出"]]
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_markdown_v2(message,
|
|
|
|
|
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
|
|
|
|
|
|
|
|
|
|
return self.CHECK_SERVER
|
|
|
|
|
|
2022-06-18 11:33:57 +00:00
|
|
|
|
@conversation_error_handler
|
2022-06-22 13:33:07 +00:00
|
|
|
|
@restricts(return_data=ConversationHandler.END)
|
2022-07-07 01:36:34 +00:00
|
|
|
|
async def check_server(self, update: Update, context: PaimonContext) -> int:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
user = update.effective_user
|
2022-07-07 01:36:34 +00:00
|
|
|
|
service = context.service
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies_command_data: CookiesCommandData = context.chat_data.get("cookies_command_data")
|
2022-07-07 01:36:34 +00:00
|
|
|
|
user_info = await service.user_service_db.get_user_info(user.id)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies_command_data.user_info = user_info
|
|
|
|
|
if update.message.text == "退出":
|
2022-04-21 11:46:28 +00:00
|
|
|
|
await update.message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-04-30 06:51:24 +00:00
|
|
|
|
elif update.message.text == "米游社":
|
2022-06-19 11:37:31 +00:00
|
|
|
|
cookies_command_data.service = ServiceEnum.HYPERION
|
2022-04-14 07:18:45 +00:00
|
|
|
|
bbs_url = "https://bbs.mihoyo.com/ys/"
|
|
|
|
|
bbs_name = "米游社"
|
|
|
|
|
if len(user_info.mihoyo_cookie) > 1:
|
|
|
|
|
await update.message.reply_text("警告,你已经绑定Cookie,如果继续操作会覆盖当前Cookie。")
|
|
|
|
|
elif update.message.text == "HoYoLab":
|
|
|
|
|
bbs_url = "https://www.hoyolab.com/home"
|
|
|
|
|
bbs_name = "HoYoLab"
|
|
|
|
|
cookies_command_data.service = ServiceEnum.HOYOLAB
|
|
|
|
|
if len(user_info.hoyoverse_cookie) > 1:
|
|
|
|
|
await update.message.reply_text("警告,你已经绑定Cookie,如果继续操作会覆盖当前Cookie。")
|
|
|
|
|
else:
|
|
|
|
|
await update.message.reply_text("选择错误,请重新选择")
|
|
|
|
|
return self.CHECK_SERVER
|
|
|
|
|
await update.message.reply_text(f"请输入{bbs_name}的Cookies!或回复退出取消操作", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
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)})(); "
|
2022-05-28 16:34:55 +00:00
|
|
|
|
javascript_android = "javascript:(()=>{prompt('',document.cookie)})();"
|
|
|
|
|
help_message = f"*关于如何获取Cookies*\n\n" \
|
|
|
|
|
f"PC:\n" \
|
2022-04-14 07:18:45 +00:00
|
|
|
|
f"[1、打开{bbs_name}并登录]({bbs_url})\n" \
|
|
|
|
|
f"2、按F12打开开发者工具\n" \
|
|
|
|
|
f"3、{escape_markdown('将开发者工具切换至控制台(Console)页签', version=2)}\n" \
|
|
|
|
|
f"4、复制下方的代码,并将其粘贴在控制台中,按下回车\n" \
|
2022-05-28 16:34:55 +00:00
|
|
|
|
f"`{escape_markdown(javascript, version=2, entity_type='code')}`\n\n" \
|
|
|
|
|
f"Android:\n" \
|
2022-06-18 11:22:47 +00:00
|
|
|
|
f"[1、通过 Via 浏览器打开{bbs_name}并登录]({bbs_url})\n" \
|
2022-05-28 16:34:55 +00:00
|
|
|
|
f"2、复制下方的代码,并将其粘贴在地址栏中,点击右侧箭头\n" \
|
|
|
|
|
f"`{escape_markdown(javascript_android, version=2, entity_type='code')}`"
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_markdown_v2(help_message, disable_web_page_preview=True)
|
|
|
|
|
return self.CHECK_COOKIES
|
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
|
@conversation_error_handler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
async def check_cookies(self, update: Update, context: CallbackContext) -> int:
|
2022-05-18 11:18:21 +00:00
|
|
|
|
user = update.effective_user
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies_command_data: CookiesCommandData = context.chat_data.get("cookies_command_data")
|
|
|
|
|
if update.message.text == "退出":
|
|
|
|
|
await update.message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
str_cookies = update.message.text
|
|
|
|
|
cookie = SimpleCookie()
|
2022-06-21 09:58:35 +00:00
|
|
|
|
try:
|
|
|
|
|
cookie.load(str_cookies)
|
|
|
|
|
except CookieError:
|
|
|
|
|
await update.message.reply_text("Cookies格式有误,请检查", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
if len(cookie) == 0:
|
|
|
|
|
await update.message.reply_text("Cookies格式有误,请检查", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
cookies = {}
|
|
|
|
|
for key, morsel in cookie.items():
|
|
|
|
|
cookies[key] = morsel.value
|
|
|
|
|
if len(cookies) == 0:
|
|
|
|
|
await update.message.reply_text("Cookies格式有误,请检查", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-06-19 11:37:31 +00:00
|
|
|
|
if cookies_command_data.service == ServiceEnum.HYPERION:
|
2022-08-09 15:36:12 +00:00
|
|
|
|
client = genshin.Client(cookies=cookies, game=types.Game.GENSHIN, region=types.Region.CHINESE)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
elif cookies_command_data.service == ServiceEnum.HOYOLAB:
|
2022-08-09 15:36:12 +00:00
|
|
|
|
client = genshin.Client(cookies=cookies,
|
|
|
|
|
game=types.Game.GENSHIN, region=types.Region.OVERSEAS, lang="zh-cn")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
else:
|
|
|
|
|
await update.message.reply_text("数据错误", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
try:
|
2022-08-09 15:36:12 +00:00
|
|
|
|
game_accounts = await client.get_game_accounts()
|
2022-06-01 03:53:31 +00:00
|
|
|
|
except DataNotPublic:
|
|
|
|
|
await update.message.reply_text("账号疑似被注销,请检查账号状态", reply_markup=ReplyKeyboardRemove())
|
2022-05-30 15:46:39 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
except InvalidCookies:
|
|
|
|
|
await update.message.reply_text("Cookies已经过期,请检查是否正确", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-06-01 03:53:31 +00:00
|
|
|
|
except GenshinException as error:
|
|
|
|
|
await update.message.reply_text(f"获取账号信息发生错误,错误信息为 {str(error)},请检查Cookie或者账号是否正常",
|
|
|
|
|
reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-06-12 10:16:52 +00:00
|
|
|
|
except AttributeError:
|
|
|
|
|
await update.message.reply_text("Cookies错误,请检查是否正确", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except ValueError:
|
|
|
|
|
await update.message.reply_text("Cookies错误,请检查是否正确", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-08-09 15:36:12 +00:00
|
|
|
|
for game_account in game_accounts:
|
|
|
|
|
if game_account.game == types.Game.GENSHIN:
|
|
|
|
|
user_info = game_account
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
await update.message.reply_text("未找到原神账号", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies_command_data.cookies = cookies
|
|
|
|
|
cookies_command_data.game_uid = user_info.uid
|
|
|
|
|
reply_keyboard = [['确认', '退出']]
|
|
|
|
|
await update.message.reply_text("获取角色基础信息成功,请检查是否正确!")
|
2022-05-18 11:18:21 +00:00
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 获取账号 {user_info.nickname}[{user_info.uid}] 信息成功")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
message = f"*角色信息*\n" \
|
2022-06-06 06:33:22 +00:00
|
|
|
|
f"角色名称:{escape_markdown(user_info.nickname, version=2)}\n" \
|
2022-04-14 07:18:45 +00:00
|
|
|
|
f"角色等级:{user_info.level}\n" \
|
|
|
|
|
f"UID:`{user_info.uid}`\n" \
|
|
|
|
|
f"服务器名称:`{user_info.server_name}`\n"
|
|
|
|
|
await update.message.reply_markdown_v2(message,
|
|
|
|
|
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
|
|
|
|
|
return self.COMMAND_RESULT
|
|
|
|
|
|
2022-06-18 11:33:57 +00:00
|
|
|
|
@conversation_error_handler
|
2022-07-07 01:36:34 +00:00
|
|
|
|
async def command_result(self, update: Update, context: PaimonContext) -> int:
|
2022-05-18 11:18:21 +00:00
|
|
|
|
user = update.effective_user
|
2022-07-07 01:36:34 +00:00
|
|
|
|
service = context.service
|
2022-04-14 07:18:45 +00:00
|
|
|
|
cookies_command_data: CookiesCommandData = context.chat_data.get("cookies_command_data")
|
|
|
|
|
if update.message.text == "退出":
|
|
|
|
|
await update.message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
elif update.message.text == "确认":
|
|
|
|
|
data = ujson.dumps(cookies_command_data.cookies)
|
|
|
|
|
user_info = cookies_command_data.user_info
|
2022-07-07 01:36:34 +00:00
|
|
|
|
game_service = ServiceEnum.NULL.value
|
2022-06-19 11:37:31 +00:00
|
|
|
|
if cookies_command_data.service == ServiceEnum.HYPERION:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
user_info.mihoyo_game_uid = cookies_command_data.game_uid
|
2022-07-07 01:36:34 +00:00
|
|
|
|
game_service = ServiceEnum.HYPERION.value
|
2022-04-14 07:18:45 +00:00
|
|
|
|
elif cookies_command_data.service == ServiceEnum.HOYOLAB:
|
|
|
|
|
user_info.hoyoverse_game_uid = cookies_command_data.game_uid
|
2022-07-07 01:36:34 +00:00
|
|
|
|
game_service = ServiceEnum.HOYOLAB.value
|
|
|
|
|
await service.user_service_db.set_user_info(user.id, user_info.mihoyo_game_uid,
|
|
|
|
|
user_info.hoyoverse_game_uid,
|
|
|
|
|
game_service)
|
|
|
|
|
await service.user_service_db.set_cookie(user.id, data, cookies_command_data.service)
|
2022-05-18 11:18:21 +00:00
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 绑定账号成功")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_text("保存成功", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
else:
|
|
|
|
|
await update.message.reply_text("回复错误,请重新输入")
|
|
|
|
|
return self.COMMAND_RESULT
|