2022-04-14 07:18:45 +00:00
|
|
|
|
import os
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
import genshin
|
2022-06-03 07:54:56 +00:00
|
|
|
|
from genshin import DataNotPublic, GenshinException
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
|
from telegram.constants import ChatAction
|
2022-06-09 05:26:08 +00:00
|
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, ConversationHandler, filters, \
|
|
|
|
|
CallbackQueryHandler
|
2022-04-14 07:18:45 +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
|
|
|
|
|
from model.helpers import url_to_file
|
2022-06-22 13:33:07 +00:00
|
|
|
|
from plugins.base import BasePlugins, restricts
|
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 import BaseService
|
|
|
|
|
from service.base import UserInfoData
|
|
|
|
|
|
|
|
|
|
|
2022-05-28 12:28:12 +00:00
|
|
|
|
class UidCommandData:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
user_info: UserInfoData = UserInfoData()
|
|
|
|
|
|
|
|
|
|
|
2022-07-07 01:36:34 +00:00
|
|
|
|
@listener_plugins_class(need_service=True)
|
2022-05-28 12:28:12 +00:00
|
|
|
|
class Uid(BasePlugins):
|
2022-06-09 12:52:59 +00:00
|
|
|
|
"""
|
|
|
|
|
玩家查询
|
|
|
|
|
"""
|
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
COMMAND_RESULT, = range(10200, 10201)
|
|
|
|
|
|
|
|
|
|
def __init__(self, service: BaseService):
|
2022-07-07 01:36:34 +00:00
|
|
|
|
self.service = service
|
2022-04-14 07:18:45 +00:00
|
|
|
|
self.current_dir = os.getcwd()
|
|
|
|
|
|
2022-07-07 01:36:34 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def create_handlers(cls, service: BaseService):
|
|
|
|
|
uid = cls(service)
|
2022-06-09 05:26:08 +00:00
|
|
|
|
uid_handler = ConversationHandler(
|
|
|
|
|
entry_points=[CommandHandler('uid', uid.command_start, block=True),
|
2022-06-09 07:12:06 +00:00
|
|
|
|
MessageHandler(filters.Regex(r"^玩家查询(.*)"), uid.command_start, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
states={
|
|
|
|
|
uid.COMMAND_RESULT: [CallbackQueryHandler(uid.command_result, block=True)]
|
|
|
|
|
},
|
|
|
|
|
fallbacks=[CommandHandler('cancel', uid.cancel, block=True)]
|
|
|
|
|
)
|
2022-06-26 06:17:43 +00:00
|
|
|
|
return [uid_handler]
|
2022-06-09 05:26:08 +00:00
|
|
|
|
|
2022-05-22 10:14:56 +00:00
|
|
|
|
async def _start_get_user_info(self, user_info_data: UserInfoData, service: ServiceEnum, uid: int = -1) -> bytes:
|
2022-06-19 11:37:31 +00:00
|
|
|
|
if service == ServiceEnum.HYPERION:
|
2022-05-22 10:14:56 +00:00
|
|
|
|
client = genshin.ChineseClient(cookies=user_info_data.mihoyo_cookie)
|
2022-05-19 16:22:46 +00:00
|
|
|
|
if uid <= 0:
|
2022-05-22 10:14:56 +00:00
|
|
|
|
_uid = user_info_data.mihoyo_game_uid
|
|
|
|
|
else:
|
|
|
|
|
_uid = uid
|
2022-04-14 07:18:45 +00:00
|
|
|
|
else:
|
2022-05-22 10:14:56 +00:00
|
|
|
|
client = genshin.GenshinClient(cookies=user_info_data.hoyoverse_cookie, lang="zh-cn")
|
2022-05-19 16:22:46 +00:00
|
|
|
|
if uid <= 0:
|
2022-05-22 10:14:56 +00:00
|
|
|
|
_uid = user_info_data.hoyoverse_game_uid
|
|
|
|
|
else:
|
|
|
|
|
_uid = uid
|
2022-05-19 16:18:07 +00:00
|
|
|
|
try:
|
2022-05-22 10:14:56 +00:00
|
|
|
|
user_info = await client.get_user(_uid)
|
2022-05-19 16:18:07 +00:00
|
|
|
|
except GenshinException as error:
|
2022-06-03 07:54:56 +00:00
|
|
|
|
Log.warning("get_record_card请求失败 \n", error)
|
2022-05-19 16:18:07 +00:00
|
|
|
|
raise error
|
2022-06-24 08:49:55 +00:00
|
|
|
|
if user_info.teapot is None:
|
|
|
|
|
raise ValueError("洞庭湖未解锁")
|
2022-05-19 16:18:07 +00:00
|
|
|
|
try:
|
2022-05-22 10:14:56 +00:00
|
|
|
|
# 查询的UID如果是自己的,会返回DataNotPublic,自己查不了自己可还行......
|
|
|
|
|
if uid > 0:
|
|
|
|
|
record_card_info = await client.get_record_card(uid)
|
|
|
|
|
else:
|
|
|
|
|
record_card_info = await client.get_record_card()
|
|
|
|
|
except DataNotPublic as error:
|
|
|
|
|
Log.warning("get_record_card请求失败 查询的用户数据未公开 \n", error)
|
|
|
|
|
nickname = _uid
|
2022-05-19 16:18:07 +00:00
|
|
|
|
user_uid = ""
|
|
|
|
|
except GenshinException as error:
|
2022-06-03 07:54:56 +00:00
|
|
|
|
Log.warning("get_record_card请求失败 \n", error)
|
2022-05-19 16:18:07 +00:00
|
|
|
|
raise error
|
|
|
|
|
else:
|
|
|
|
|
nickname = record_card_info.nickname
|
|
|
|
|
user_uid = record_card_info.uid
|
2022-04-14 07:18:45 +00:00
|
|
|
|
user_avatar = user_info.characters[0].icon
|
|
|
|
|
user_data = {
|
2022-05-19 16:18:07 +00:00
|
|
|
|
"name": nickname,
|
|
|
|
|
"uid": user_uid,
|
2022-04-14 07:18:45 +00:00
|
|
|
|
"user_avatar": await url_to_file(user_avatar),
|
|
|
|
|
"action_day_number": user_info.stats.days_active,
|
|
|
|
|
"achievement_number": user_info.stats.achievements,
|
2022-05-28 14:36:54 +00:00
|
|
|
|
"avatar_number": user_info.stats.characters,
|
2022-04-14 07:18:45 +00:00
|
|
|
|
"spiral_abyss": user_info.stats.spiral_abyss,
|
|
|
|
|
"way_point_number": user_info.stats.unlocked_waypoints,
|
|
|
|
|
"domain_number": user_info.stats.unlocked_domains,
|
|
|
|
|
"luxurious_number": user_info.stats.luxurious_chests,
|
|
|
|
|
"precious_chest_number": user_info.stats.precious_chests,
|
|
|
|
|
"exquisite_chest_number": user_info.stats.exquisite_chests,
|
|
|
|
|
"common_chest_number": user_info.stats.common_chests,
|
|
|
|
|
"magic_chest_number": user_info.stats.remarkable_chests,
|
|
|
|
|
"anemoculus_number": user_info.stats.anemoculi,
|
|
|
|
|
"geoculus_number": user_info.stats.geoculi,
|
|
|
|
|
"electroculus_number": user_info.stats.electroculi,
|
|
|
|
|
"world_exploration_list": [],
|
|
|
|
|
"teapot_level": user_info.teapot.level,
|
|
|
|
|
"teapot_comfort_num": user_info.teapot.comfort,
|
|
|
|
|
"teapot_item_num": user_info.teapot.items,
|
|
|
|
|
"teapot_visit_num": user_info.teapot.visitors,
|
|
|
|
|
"teapot_list": []
|
|
|
|
|
}
|
|
|
|
|
for exploration in user_info.explorations:
|
|
|
|
|
exploration_data = {
|
|
|
|
|
"name": exploration.name,
|
2022-05-19 12:03:33 +00:00
|
|
|
|
"exploration_percentage": exploration.explored,
|
2022-04-14 07:18:45 +00:00
|
|
|
|
"offerings": [],
|
|
|
|
|
"icon": await url_to_file(exploration.icon)
|
|
|
|
|
}
|
|
|
|
|
for offering in exploration.offerings:
|
2022-05-26 00:02:46 +00:00
|
|
|
|
# 修复上游奇怪的问题
|
|
|
|
|
offering_name = offering.name
|
|
|
|
|
if offering_name == "Reputation":
|
|
|
|
|
offering_name = "声望等级"
|
2022-04-14 07:18:45 +00:00
|
|
|
|
offering_data = {
|
2022-05-26 00:02:46 +00:00
|
|
|
|
"data": f"{offering_name}:{offering.level}级"
|
2022-04-14 07:18:45 +00:00
|
|
|
|
}
|
|
|
|
|
exploration_data["offerings"].append(offering_data)
|
|
|
|
|
user_data["world_exploration_list"].append(exploration_data)
|
|
|
|
|
for teapot in user_info.teapot.realms:
|
2022-05-26 00:02:46 +00:00
|
|
|
|
teapot_icon = teapot.icon
|
|
|
|
|
# 修复 国际服绘绮庭 图标 地址请求 为404
|
|
|
|
|
if "UI_HomeworldModule_4_Pic.png" in teapot_icon:
|
|
|
|
|
teapot_icon = "https://upload-bbs.mihoyo.com/game_record/genshin/home/UI_HomeworldModule_4_Pic.png"
|
2022-04-14 07:18:45 +00:00
|
|
|
|
teapot_data = {
|
2022-05-26 00:02:46 +00:00
|
|
|
|
"icon": await url_to_file(teapot_icon),
|
2022-04-14 07:18:45 +00:00
|
|
|
|
"name": teapot.name
|
|
|
|
|
}
|
|
|
|
|
user_data["teapot_list"].append(teapot_data)
|
|
|
|
|
background_image = random.choice(os.listdir(f"{self.current_dir}/resources/background/vertical"))
|
|
|
|
|
user_data["background_image"] = f"file://{self.current_dir}/resources/background/vertical/{background_image}"
|
|
|
|
|
png_data = await self.service.template.render('genshin/info', "info.html", user_data,
|
|
|
|
|
{"width": 1024, "height": 1024})
|
2022-04-30 07:45:05 +00:00
|
|
|
|
return png_data
|
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
|
@conversation_error_handler
|
2022-06-22 13:33:07 +00:00
|
|
|
|
@restricts(return_data=ConversationHandler.END)
|
2022-04-30 07:45:05 +00:00
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
2022-05-19 12:03:33 +00:00
|
|
|
|
message = update.message
|
2022-04-30 07:45:05 +00:00
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 查询游戏用户命令请求")
|
2022-05-28 12:28:12 +00:00
|
|
|
|
uid_command_data: UidCommandData = context.chat_data.get("uid_command_data")
|
|
|
|
|
if uid_command_data is None:
|
|
|
|
|
uid_command_data = UidCommandData()
|
|
|
|
|
context.chat_data["uid_command_data"] = uid_command_data
|
2022-04-30 07:45:05 +00:00
|
|
|
|
user_info = await self.service.user_service_db.get_user_info(user.id)
|
|
|
|
|
if user_info.user_id == 0:
|
2022-05-30 15:41:14 +00:00
|
|
|
|
reply_message = await message.reply_text("未查询到账号信息,请先私聊派蒙绑定账号")
|
2022-05-28 14:53:03 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 300)
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 300)
|
2022-05-19 12:03:33 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
uid: int = -1
|
|
|
|
|
try:
|
2022-06-05 08:13:10 +00:00
|
|
|
|
args = context.args
|
2022-06-21 13:29:44 +00:00
|
|
|
|
if args is not None:
|
|
|
|
|
if len(args) >= 1:
|
|
|
|
|
uid = int(args[0])
|
2022-05-19 12:03:33 +00:00
|
|
|
|
except ValueError as error:
|
2022-06-10 08:47:32 +00:00
|
|
|
|
Log.error("获取 chat_id 发生错误! 错误信息为", error)
|
2022-05-19 12:03:33 +00:00
|
|
|
|
await message.reply_text("输入错误")
|
2022-04-30 07:45:05 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
if user_info.service == ServiceEnum.NULL:
|
|
|
|
|
reply_text = "请选择你要查询的类别"
|
|
|
|
|
keyboard = [
|
|
|
|
|
[
|
2022-05-28 12:28:12 +00:00
|
|
|
|
InlineKeyboardButton("米游社", callback_data="uid|米游社"),
|
|
|
|
|
InlineKeyboardButton("HoYoLab", callback_data="uid|HoYoLab")
|
2022-04-30 07:45:05 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
2022-05-28 12:28:12 +00:00
|
|
|
|
uid_command_data.user_info = user_info
|
2022-06-24 08:49:55 +00:00
|
|
|
|
await message.reply_text(reply_text, reply_markup=InlineKeyboardMarkup(keyboard))
|
2022-04-30 07:45:05 +00:00
|
|
|
|
return self.COMMAND_RESULT
|
|
|
|
|
else:
|
2022-06-24 08:49:55 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-06-14 16:14:24 +00:00
|
|
|
|
try:
|
|
|
|
|
png_data = await self._start_get_user_info(user_info, user_info.service, uid)
|
2022-06-24 08:49:55 +00:00
|
|
|
|
except ValueError as exc:
|
|
|
|
|
if "洞庭湖未解锁" in str(exc):
|
2022-06-24 14:31:41 +00:00
|
|
|
|
await message.reply_text("角色尘歌壶未解锁 如果想要查看具体数据 嗯...... 咕咕咕~")
|
2022-06-24 08:49:55 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
else:
|
|
|
|
|
raise exc
|
|
|
|
|
except AttributeError as exc:
|
|
|
|
|
Log.warning("角色数据有误", exc)
|
|
|
|
|
await message.reply_text("角色数据有误 估计是派蒙晕了")
|
2022-06-14 16:14:24 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-24 08:49:55 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
|
|
|
|
await message.reply_photo(png_data, filename=f"{user_info.user_id}.png", allow_sending_without_reply=True)
|
2022-04-30 07:45:05 +00:00
|
|
|
|
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
|
@conversation_error_handler
|
2022-04-30 07:45:05 +00:00
|
|
|
|
async def command_result(self, update: Update, context: CallbackContext) -> int:
|
2022-05-28 12:28:12 +00:00
|
|
|
|
get_user_command_data: UidCommandData = context.chat_data["uid_command_data"]
|
2022-04-30 07:45:05 +00:00
|
|
|
|
query = update.callback_query
|
2022-06-24 08:49:55 +00:00
|
|
|
|
message = query.message
|
2022-04-30 07:45:05 +00:00
|
|
|
|
await query.answer()
|
|
|
|
|
await query.delete_message()
|
2022-05-28 12:28:12 +00:00
|
|
|
|
if query.data == "uid|米游社":
|
2022-06-19 11:37:31 +00:00
|
|
|
|
service = ServiceEnum.HYPERION
|
2022-05-28 12:28:12 +00:00
|
|
|
|
elif query.data == "uid|HoYoLab":
|
2022-04-30 07:45:05 +00:00
|
|
|
|
service = ServiceEnum.HOYOLAB
|
|
|
|
|
else:
|
|
|
|
|
return ConversationHandler.END
|
2022-06-14 16:14:24 +00:00
|
|
|
|
try:
|
|
|
|
|
png_data = await self._start_get_user_info(get_user_command_data.user_info, service)
|
2022-06-24 08:49:55 +00:00
|
|
|
|
except ValueError as exc:
|
|
|
|
|
if "洞庭湖未解锁" in str(exc):
|
2022-06-24 14:31:41 +00:00
|
|
|
|
await message.reply_text("角色尘歌壶未解锁 如果想要查看具体数据 嗯...... 咕咕咕~")
|
2022-06-24 08:49:55 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
else:
|
|
|
|
|
raise exc
|
|
|
|
|
except AttributeError as exc:
|
|
|
|
|
Log.warning("角色数据有误", exc)
|
|
|
|
|
await message.reply_text("角色数据有误 估计是派蒙晕了")
|
2022-06-14 16:14:24 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-24 08:49:55 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
|
|
|
|
await message.reply_photo(png_data, filename=f"{get_user_command_data.user_info.user_id}.png",
|
|
|
|
|
allow_sending_without_reply=True)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return ConversationHandler.END
|