PaiGram/plugins/strategy.py

61 lines
2.9 KiB
Python
Raw Normal View History

2022-06-03 07:58:51 +00:00
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
2022-05-28 07:50:19 +00:00
from telegram.constants import ChatAction, ParseMode
from telegram.ext import CallbackContext, filters, ConversationHandler
2022-05-28 07:50:19 +00:00
from logger import Log
2022-06-09 07:12:06 +00:00
from metadata.shortname import roleToName
2022-05-28 07:50:19 +00:00
from model.helpers import url_to_file
2022-06-22 13:33:07 +00:00
from plugins.base import BasePlugins, restricts
from plugins.errorhandler import conversation_error_handler
2022-05-28 07:50:19 +00:00
class Strategy(BasePlugins):
2022-06-09 12:52:59 +00:00
"""
角色攻略查询
"""
2022-05-28 07:50:19 +00:00
2022-06-24 07:57:24 +00:00
KEYBOARD = [[InlineKeyboardButton(text="查看角色攻略列表并查询", switch_inline_query_current_chat="查看角色攻略列表并查询")]]
@conversation_error_handler
2022-06-22 13:33:07 +00:00
@restricts(return_data=ConversationHandler.END)
async def command_start(self, update: Update, context: CallbackContext) -> None:
2022-05-28 07:50:19 +00:00
message = update.message
user = update.effective_user
2022-06-21 13:29:44 +00:00
args = context.args
2022-06-24 07:57:24 +00:00
match = context.match
role_name: str = ""
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 != "":
role_name = match_data
2022-05-28 07:50:19 +00:00
else:
2022-06-24 07:57:24 +00:00
if len(args) >= 1:
role_name = args[0]
await update.message.reply_chat_action(ChatAction.TYPING)
if role_name == "":
reply_message = await message.reply_text("请回复你要查询的攻略的角色名",
2022-06-24 07:57:24 +00:00
reply_markup=InlineKeyboardMarkup(self.KEYBOARD))
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-05-28 07:50:19 +00:00
return
2022-06-24 07:57:24 +00:00
role_name = roleToName(role_name)
url = await self.service.get_game_info.get_characters_cultivation_atlas(role_name)
2022-05-28 07:50:19 +00:00
if url == "":
reply_message = await message.reply_text(f"没有找到 {role_name} 的攻略",
2022-06-24 07:57:24 +00:00
reply_markup=InlineKeyboardMarkup(self.KEYBOARD))
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-05-28 07:50:19 +00:00
return
Log.info(f"用户 {user.full_name}[{user.id}] 查询角色攻略命令请求 || 参数 {role_name}")
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
file_path = await url_to_file(url, "")
2022-06-09 12:52:59 +00:00
caption = "Form 米游社 西风驿站 " \
2022-05-28 07:50:19 +00:00
f"查看 [原图]({url})"
await message.reply_photo(photo=open(file_path, "rb"), caption=caption, filename=f"{role_name}.png",
allow_sending_without_reply=True, parse_mode=ParseMode.MARKDOWN_V2)