PaiGram/plugins/strategy.py
洛水.山岭居室 693cd774d3
重构插件传入 service 方式
* 重构插件传入 `service` 方式

重构插件传入 `service` 方式
将 `create_handlers`函数的修饰符改为 `classmethod`
2022-07-07 09:36:34 +08:00

73 lines
3.3 KiB
Python

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ChatAction, ParseMode
from telegram.ext import filters, ConversationHandler, CommandHandler, MessageHandler
from logger import Log
from manager import listener_plugins_class
from metadata.shortname import roleToName
from model.helpers import url_to_file
from plugins.base import BasePlugins, restricts
from plugins.errorhandler import conversation_error_handler
from utils.base import PaimonContext
@listener_plugins_class()
class Strategy(BasePlugins):
"""
角色攻略查询
"""
KEYBOARD = [[InlineKeyboardButton(text="查看角色攻略列表并查询", switch_inline_query_current_chat="查看角色攻略列表并查询")]]
@classmethod
def create_handlers(cls) -> list:
strategy = cls()
return [
CommandHandler("strategy", strategy.command_start, block=False),
MessageHandler(filters.Regex("^角色攻略查询(.*)"), strategy.command_start, block=False),
]
@conversation_error_handler
@restricts(return_data=ConversationHandler.END)
async def command_start(self, update: Update, context: PaimonContext) -> None:
message = update.message
user = update.effective_user
args = context.args
match = context.match
service = context.service
role_name: str = ""
if args is None:
if match is not None:
match_data = match.group(1)
if match_data != "":
role_name = match_data
else:
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("请回复你要查询的攻略的角色名",
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)
return
role_name = roleToName(role_name)
url = await service.get_game_info.get_characters_cultivation_atlas(role_name)
if url == "":
reply_message = await message.reply_text(f"没有找到 {role_name} 的攻略",
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)
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, "")
caption = "Form 米游社 西风驿站 " \
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)