2023-03-14 01:27:22 +00:00
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
|
|
|
from telegram.constants import ChatAction, ParseMode
|
|
|
|
from telegram.ext import CallbackContext, filters
|
2022-07-31 07:38:31 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2023-05-02 16:16:14 +00:00
|
|
|
from core.services.game.services import GameCacheService
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.services.search.models import StrategyEntry
|
|
|
|
from core.services.search.services import SearchServices
|
2023-04-26 08:48:05 +00:00
|
|
|
from core.services.wiki.services import WikiService
|
2023-12-27 07:12:41 +00:00
|
|
|
from metadata.shortname import roleToName, roleToTag, roleToId
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-31 07:38:31 +00:00
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
class StrategyPlugin(Plugin):
|
2022-07-31 07:38:31 +00:00
|
|
|
"""角色攻略查询"""
|
|
|
|
|
2024-03-16 10:43:20 +00:00
|
|
|
KEYBOARD = [
|
|
|
|
[InlineKeyboardButton(text="查看角色攻略列表并查询", switch_inline_query_current_chat="查看角色攻略列表并查询")]
|
|
|
|
]
|
2022-07-31 07:38:31 +00:00
|
|
|
|
2022-12-04 11:56:39 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-02 16:16:14 +00:00
|
|
|
cache_service: GameCacheService = None,
|
2023-04-26 08:48:05 +00:00
|
|
|
wiki_service: WikiService = None,
|
2022-12-04 11:56:39 +00:00
|
|
|
search_service: SearchServices = None,
|
|
|
|
):
|
2023-04-26 08:48:05 +00:00
|
|
|
self.cache_service = cache_service
|
|
|
|
self.wiki_service = wiki_service
|
2022-12-04 11:56:39 +00:00
|
|
|
self.search_service = search_service
|
2022-07-31 07:38:31 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
@handler.command(command="strategy", block=False)
|
|
|
|
@handler.message(filters=filters.Regex("^角色攻略查询(.*)"), block=False)
|
2022-07-31 07:38:31 +00:00
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
2022-09-08 01:08:37 +00:00
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
args = self.get_args(context)
|
2022-07-31 07:38:31 +00:00
|
|
|
if len(args) >= 1:
|
|
|
|
character_name = args[0]
|
|
|
|
else:
|
2024-03-16 10:43:20 +00:00
|
|
|
reply_message = await message.reply_text(
|
|
|
|
"请回复你要查询的攻略的角色名", reply_markup=InlineKeyboardMarkup(self.KEYBOARD)
|
|
|
|
)
|
2022-07-31 07:38:31 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message)
|
|
|
|
self.add_delete_message_job(reply_message)
|
2022-07-31 07:38:31 +00:00
|
|
|
return
|
2022-09-09 15:50:10 +00:00
|
|
|
character_name = roleToName(character_name)
|
2023-12-27 07:12:41 +00:00
|
|
|
character_id = roleToId(character_name)
|
|
|
|
file_path = self.wiki_service.raider.raider_guide_for_role_path / f"{character_id}.png"
|
2023-04-26 08:48:05 +00:00
|
|
|
if not file_path.exists():
|
2022-07-31 07:38:31 +00:00
|
|
|
reply_message = await message.reply_text(
|
|
|
|
f"没有找到 {character_name} 的攻略", reply_markup=InlineKeyboardMarkup(self.KEYBOARD)
|
2022-10-10 11:07:28 +00:00
|
|
|
)
|
2022-07-31 07:38:31 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message)
|
|
|
|
self.add_delete_message_job(reply_message)
|
2022-07-31 07:38:31 +00:00
|
|
|
return
|
2024-03-10 12:50:32 +00:00
|
|
|
self.log_user(update, logger.info, "查询角色攻略命令请求 || 参数 %s", character_name)
|
2022-07-31 07:38:31 +00:00
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2023-05-02 16:16:14 +00:00
|
|
|
caption = "From 米游社@听语惊花"
|
2023-04-26 08:48:05 +00:00
|
|
|
if file_id := await self.cache_service.get_strategy_cache(character_name):
|
2023-05-02 16:16:14 +00:00
|
|
|
await message.reply_photo(
|
|
|
|
photo=file_id,
|
2022-12-04 11:56:39 +00:00
|
|
|
caption=caption,
|
2023-04-26 08:48:05 +00:00
|
|
|
filename=f"{character_name}.png",
|
|
|
|
parse_mode=ParseMode.HTML,
|
2022-12-04 11:56:39 +00:00
|
|
|
)
|
2023-04-26 08:48:05 +00:00
|
|
|
else:
|
2023-05-02 16:16:14 +00:00
|
|
|
reply_photo = await message.reply_photo(
|
|
|
|
photo=open(file_path, "rb"),
|
2023-04-26 08:48:05 +00:00
|
|
|
caption=caption,
|
|
|
|
filename=f"{character_name}.png",
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
)
|
2023-05-02 16:16:14 +00:00
|
|
|
if reply_photo.photo:
|
2023-04-26 08:48:05 +00:00
|
|
|
tags = roleToTag(character_name)
|
2023-05-02 16:16:14 +00:00
|
|
|
photo_file_id = reply_photo.photo[0].file_id
|
|
|
|
await self.cache_service.set_strategy_cache(character_name, photo_file_id)
|
2023-04-26 08:48:05 +00:00
|
|
|
entry = StrategyEntry(
|
|
|
|
key=f"plugin:strategy:{character_name}",
|
|
|
|
title=character_name,
|
|
|
|
description=f"{character_name} 角色攻略",
|
|
|
|
tags=tags,
|
|
|
|
caption=caption,
|
|
|
|
parse_mode="HTML",
|
2023-05-02 16:16:14 +00:00
|
|
|
photo_file_id=photo_file_id,
|
2023-04-26 08:48:05 +00:00
|
|
|
)
|
|
|
|
await self.search_service.add_entry(entry)
|