2022-04-14 07:18:45 +00:00
|
|
|
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
from telegram.constants import ChatAction
|
2022-07-07 01:36:34 +00:00
|
|
|
from telegram.ext import filters, CommandHandler, MessageHandler
|
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-06-09 07:12:06 +00:00
|
|
|
from metadata.shortname import weaponToName
|
2022-04-14 07:18:45 +00:00
|
|
|
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-07-07 04:30:35 +00:00
|
|
|
from service import BaseService
|
2022-07-07 01:36:34 +00:00
|
|
|
from utils.base import PaimonContext
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
2022-07-07 04:30:35 +00:00
|
|
|
@listener_plugins_class(need_service=True)
|
2022-04-14 07:18:45 +00:00
|
|
|
class Weapon(BasePlugins):
|
2022-06-09 12:52:59 +00:00
|
|
|
"""
|
|
|
|
武器查询
|
|
|
|
"""
|
2022-04-14 07:18:45 +00:00
|
|
|
|
2022-06-24 07:57:24 +00:00
|
|
|
KEYBOARD = [[InlineKeyboardButton(text="查看武器列表并查询", switch_inline_query_current_chat="查看武器列表并查询")]]
|
|
|
|
|
2022-07-07 04:30:35 +00:00
|
|
|
def __init__(self, service: BaseService):
|
|
|
|
self.service = service
|
|
|
|
|
2022-07-07 01:36:34 +00:00
|
|
|
@classmethod
|
2022-07-07 04:30:35 +00:00
|
|
|
def create_handlers(cls, service: BaseService) -> list:
|
|
|
|
weapon = cls(service)
|
2022-06-26 06:17:43 +00:00
|
|
|
return [
|
|
|
|
CommandHandler("weapon", weapon.command_start, block=False),
|
|
|
|
MessageHandler(filters.Regex("^武器查询(.*)"), weapon.command_start, block=False)
|
|
|
|
]
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
@conversation_error_handler
|
2022-06-22 13:33:07 +00:00
|
|
|
@restricts()
|
2022-07-07 01:36:34 +00:00
|
|
|
async def command_start(self, update: Update, context: PaimonContext) -> None:
|
2022-04-14 07:18:45 +00:00
|
|
|
message = update.message
|
|
|
|
user = update.effective_user
|
2022-06-05 08:13:10 +00:00
|
|
|
args = context.args
|
2022-06-24 07:57:24 +00:00
|
|
|
match = context.match
|
2022-06-21 13:29:44 +00:00
|
|
|
weapon_name: str = ""
|
2022-06-24 07:57:24 +00:00
|
|
|
if args is None:
|
|
|
|
if match is not None:
|
2022-06-24 12:26:15 +00:00
|
|
|
match_data = match.group(1)
|
|
|
|
if match_data != "":
|
|
|
|
weapon_name = match_data
|
2022-06-24 07:57:24 +00:00
|
|
|
else:
|
2022-06-21 13:29:44 +00:00
|
|
|
if len(args) >= 1:
|
|
|
|
weapon_name = args[0]
|
2022-06-24 07:57:24 +00:00
|
|
|
if weapon_name == "":
|
|
|
|
reply_message = await message.reply_text("请回复你要查询的武器",
|
|
|
|
reply_markup=InlineKeyboardMarkup(self.KEYBOARD))
|
2022-05-29 16:29:24 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id)
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
|
2022-04-14 07:18:45 +00:00
|
|
|
return
|
2022-05-29 16:11:03 +00:00
|
|
|
weapon_name = weaponToName(weapon_name)
|
2022-06-15 11:54:42 +00:00
|
|
|
weapons_list = await self.service.wiki.get_weapons_list()
|
|
|
|
for weapon in weapons_list:
|
|
|
|
if weapon["name"] == weapon_name:
|
2022-04-14 07:18:45 +00:00
|
|
|
weapon_data = weapon
|
2022-06-24 07:57:24 +00:00
|
|
|
break
|
|
|
|
else:
|
2022-05-29 16:29:24 +00:00
|
|
|
reply_message = await message.reply_text(f"没有找到 {weapon_name}",
|
2022-06-24 07:57:24 +00:00
|
|
|
reply_markup=InlineKeyboardMarkup(self.KEYBOARD))
|
2022-05-29 16:29:24 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id)
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
|
2022-04-14 07:18:45 +00:00
|
|
|
return
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 查询武器命令请求 || 参数 {weapon_name}")
|
2022-05-30 08:08:56 +00:00
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
async def input_template_data(_weapon_data):
|
|
|
|
_template_data = {
|
2022-06-15 11:54:42 +00:00
|
|
|
"weapon_name": _weapon_data["name"],
|
|
|
|
"weapon_info_type_img": await url_to_file(_weapon_data["type"]["icon"]),
|
|
|
|
"progression_secondary_stat_value": _weapon_data["secondary"]["max"],
|
|
|
|
"progression_secondary_stat_name": _weapon_data["secondary"]["name"],
|
|
|
|
"weapon_info_source_img": await url_to_file(_weapon_data["source_img"]),
|
|
|
|
"progression_base_atk": _weapon_data["atk"]["max"],
|
2022-04-14 07:18:45 +00:00
|
|
|
"weapon_info_source_list": [],
|
2022-06-15 11:54:42 +00:00
|
|
|
"special_ability_name": _weapon_data["passive_ability"]["name"],
|
|
|
|
"special_ability_info": _weapon_data["passive_ability"]["description"],
|
2022-04-14 07:18:45 +00:00
|
|
|
}
|
|
|
|
_template_data["weapon_info_source_list"].append(
|
2022-06-15 11:54:42 +00:00
|
|
|
await url_to_file(_weapon_data["materials"]["ascension"]["icon"])
|
2022-04-14 07:18:45 +00:00
|
|
|
)
|
|
|
|
_template_data["weapon_info_source_list"].append(
|
2022-06-15 11:54:42 +00:00
|
|
|
await url_to_file(_weapon_data["materials"]["elite"]["icon"])
|
2022-04-14 07:18:45 +00:00
|
|
|
)
|
|
|
|
_template_data["weapon_info_source_list"].append(
|
2022-06-15 11:54:42 +00:00
|
|
|
await url_to_file(_weapon_data["materials"]["monster"]["icon"])
|
2022-04-14 07:18:45 +00:00
|
|
|
)
|
|
|
|
return _template_data
|
|
|
|
|
|
|
|
template_data = await input_template_data(weapon_data)
|
2022-07-07 04:30:35 +00:00
|
|
|
png_data = await self.service.template.render('genshin/weapon', "weapon.html", template_data,
|
|
|
|
{"width": 540, "height": 540})
|
2022-04-14 07:18:45 +00:00
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
|
|
|
await message.reply_photo(png_data, filename=f"{template_data['weapon_name']}.png",
|
|
|
|
allow_sending_without_reply=True)
|