PamGram/plugins/genshin/artifact_rate.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

148 lines
7.1 KiB
Python
Raw Normal View History

from typing import Optional, Tuple
2022-07-26 15:55:24 +00:00
from telegram import File, InlineKeyboardButton, InlineKeyboardMarkup, Update
2022-07-26 15:55:24 +00:00
from telegram.constants import ChatAction, ParseMode
from telegram.ext import CallbackContext, ConversationHandler, filters
2022-07-26 15:55:24 +00:00
from telegram.helpers import escape_markdown
from core.baseplugin import BasePlugin
from core.plugin import Plugin, conversation, handler
from modules.apihelper.artifact import ArtifactOcrRate, get_comment, get_format_sub_item
2022-07-26 15:55:24 +00:00
from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts
from utils.log import logger
2022-07-26 15:55:24 +00:00
COMMAND_RESULT = 1
2022-07-26 15:55:24 +00:00
class ArtifactRate(Plugin.Conversation, BasePlugin.Conversation):
"""圣遗物评分"""
2022-07-26 15:55:24 +00:00
STAR_KEYBOARD = [
[InlineKeyboardButton(f"{i}", callback_data=f"artifact_ocr_rate_data|star|{i}") for i in range(1, 6)]
]
LEVEL_KEYBOARD = [
[
2022-07-26 15:55:24 +00:00
InlineKeyboardButton(f"{i * 5 + j}", callback_data=f"artifact_ocr_rate_data|level|{i * 5 + j}")
for j in range(1, 6)
]
for i in range(0, 4)
]
def __init__(self):
self.artifact_rate = ArtifactOcrRate()
async def get_rate(self, artifact_attr: dict) -> str:
rate_result_req = await self.artifact_rate.rate_artifact(artifact_attr)
if rate_result_req.status_code != 200:
if rate_result_req.status_code == 400:
artifact_attr = rate_result_req.json()
return artifact_attr.get("message", "API请求错误")
2022-10-22 13:54:04 +00:00
return "API请求错误请稍后再试"
2022-07-26 15:55:24 +00:00
rate_result = rate_result_req.json()
return (
"*圣遗物评分结果*\n"
f"主属性:{escape_markdown(artifact_attr['main_item']['name'], version=2)}\n"
f"{escape_markdown(get_format_sub_item(artifact_attr), version=2)}"
f"`--------------------`\n"
f"总分:{escape_markdown(rate_result['total_percent'], version=2)}\n"
f"主词条:{escape_markdown(rate_result['main_percent'], version=2)}\n"
f"副词条:{escape_markdown(rate_result['sub_percent'], version=2)}\n"
f"`--------------------`\n"
f"{escape_markdown(get_comment(rate_result['total_percent']), version=2)}\n"
"_评分、识图均来自 genshin\\.pub_"
)
2022-07-26 15:55:24 +00:00
@conversation.entry_point
@handler.command(command="artifact_rate", filters=filters.ChatType.PRIVATE, block=True)
@handler.message(filters=filters.Regex(r"^圣遗物评分(.*)"), block=True)
@handler.message(filters=filters.CaptionRegex(r"^圣遗物评分(.*)"), block=True)
2022-07-26 15:55:24 +00:00
@error_callable
@restricts(return_data=ConversationHandler.END)
async def command_start(self, update: Update, context: CallbackContext) -> int:
message = update.effective_message
2022-07-26 15:55:24 +00:00
user = update.effective_user
logger.info(f"用户 {user.full_name}[{user.id}] 圣遗物评分命令请求")
2022-07-26 15:55:24 +00:00
context.user_data["artifact_attr"] = None
photo_file: Optional[File] = None
if message is None:
return ConversationHandler.END
2022-10-22 13:54:04 +00:00
message_data = message if message.reply_to_message is None else message.reply_to_message
if message_data.photo is not None and len(message_data.photo) >= 1:
photo_file = await message_data.photo[-1].get_file() # 草 居然第一张是预览图我人都麻了
elif message_data.document is not None:
document = message_data.document
if "image" not in document.mime_type:
await message.reply_text("错误的图片类型")
return ConversationHandler.END
if document.file_size >= 5242880:
await message.reply_text("图片太大啦")
return ConversationHandler.END
photo_file = await document.get_file()
2022-07-26 15:55:24 +00:00
if photo_file is None:
await message.reply_text("图呢?")
return ConversationHandler.END
photo_byte = await photo_file.download_as_bytearray()
artifact_attr_req = await self.artifact_rate.get_artifact_attr(photo_byte)
if artifact_attr_req.status_code != 200:
if artifact_attr_req.status_code == 400:
artifact_attr = artifact_attr_req.json()
2022-10-22 13:54:04 +00:00
await message.reply_text(artifact_attr.get("message", "API请求错误请稍后再试"))
2022-07-26 15:55:24 +00:00
return ConversationHandler.END
2022-10-22 13:54:04 +00:00
await message.reply_text("API请求错误请稍后再试")
2022-07-26 15:55:24 +00:00
return ConversationHandler.END
artifact_attr = artifact_attr_req.json()
context.user_data["artifact_attr"] = artifact_attr
if artifact_attr.get("star") is None:
await message.reply_text("无法识别圣遗物星级,请选择圣遗物星级", reply_markup=InlineKeyboardMarkup(self.STAR_KEYBOARD))
return COMMAND_RESULT
2022-07-26 15:55:24 +00:00
if artifact_attr.get("level") is None:
await message.reply_text("无法识别圣遗物等级,请选择圣遗物等级", reply_markup=InlineKeyboardMarkup(self.LEVEL_KEYBOARD))
return COMMAND_RESULT
2022-07-26 15:55:24 +00:00
reply_message = await message.reply_text("识图成功!\n" "正在评分中...")
rate_text = await self.get_rate(artifact_attr)
await reply_message.edit_text(rate_text, parse_mode=ParseMode.MARKDOWN_V2)
return ConversationHandler.END
@conversation.state(state=COMMAND_RESULT)
@handler.callback_query()
2022-07-26 15:55:24 +00:00
@error_callable
async def command_result(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
artifact_attr = context.user_data.get("artifact_attr")
await query.answer()
if artifact_attr is None:
2022-10-22 13:54:04 +00:00
await query.edit_message_text("数据错误,请重新发送图片")
2022-07-26 15:55:24 +00:00
return ConversationHandler.END
def get_callback_data(callback_query_data: str) -> Tuple[str, int]:
2022-07-26 15:55:24 +00:00
_data = callback_query_data.split("|")
_key_name = _data[1]
try:
_value = int(_data[2])
except ValueError:
_value = -1
return _key_name, _value
await query.message.reply_chat_action(ChatAction.TYPING)
key_name, value = get_callback_data(query.data)
if key_name == "level":
artifact_attr["level"] = value
elif key_name == "star":
artifact_attr["star"] = value
else:
2022-10-22 13:54:04 +00:00
await query.edit_message_text("数据错误,请重新发送图片")
2022-07-26 15:55:24 +00:00
return ConversationHandler.END
if artifact_attr.get("level") is None:
await query.edit_message_text("无法识别圣遗物等级,请选择圣遗物等级", reply_markup=InlineKeyboardMarkup(self.LEVEL_KEYBOARD))
return COMMAND_RESULT
2022-07-26 15:55:24 +00:00
if artifact_attr.get("star") is None:
await query.edit_message_text("无法识别圣遗物星级,请选择圣遗物星级", reply_markup=InlineKeyboardMarkup(self.STAR_KEYBOARD))
return COMMAND_RESULT
2022-07-26 15:55:24 +00:00
await query.edit_message_text("正在评分中...")
rate_text = await self.get_rate(artifact_attr)
await query.edit_message_text(rate_text, parse_mode=ParseMode.MARKDOWN_V2)
return ConversationHandler.END