2022-07-26 16:19:21 +00:00
|
|
|
from os import sep
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from telegram import Update
|
|
|
|
from telegram.constants import ChatAction
|
|
|
|
from telegram.ext import CommandHandler, MessageHandler, filters, CallbackContext
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.baseplugin import BasePlugin
|
|
|
|
from core.plugin import handler, Plugin
|
2022-07-26 16:19:21 +00:00
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-26 16:19:21 +00:00
|
|
|
from .model import MapHelper
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class Map(Plugin, BasePlugin):
|
2022-07-26 16:19:21 +00:00
|
|
|
"""支持资源点查询"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.init_resource_map = False
|
|
|
|
self.map_helper = MapHelper()
|
|
|
|
|
|
|
|
async def init_point_list_and_map(self):
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.info("正在初始化地图资源节点")
|
2022-07-26 16:19:21 +00:00
|
|
|
if not self.init_resource_map:
|
|
|
|
await self.map_helper.init_point_list_and_map()
|
|
|
|
self.init_resource_map = True
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
@handler(CommandHandler, command="map", block=False)
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^资源点查询(.*)"), block=False)
|
2022-07-26 16:19:21 +00:00
|
|
|
@error_callable
|
|
|
|
@restricts(restricts_time=20)
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext):
|
2022-09-18 08:35:37 +00:00
|
|
|
message = update.effective_message
|
2022-07-26 16:19:21 +00:00
|
|
|
args = context.args
|
|
|
|
user = update.effective_user
|
|
|
|
if not self.init_resource_map:
|
|
|
|
await self.init_point_list_and_map()
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
if len(args) >= 1:
|
|
|
|
resource_name = args[0]
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.info(f"用户: {user.full_name} [{user.id}] 使用了 map 命令")
|
2022-07-26 16:19:21 +00:00
|
|
|
await message.reply_text("请输入要查找的资源,或私聊派蒙发送 `/map list` 查看资源列表", parse_mode="Markdown")
|
|
|
|
return
|
|
|
|
if resource_name in ("list", "列表"):
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
reply_message = await message.reply_text("请私聊派蒙使用该命令")
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 300)
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 300)
|
|
|
|
return
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.info(f"用户: {user.full_name} [{user.id}] 使用 map 命令查询了 资源列表")
|
2022-07-26 16:19:21 +00:00
|
|
|
text = self.map_helper.get_resource_list_mes()
|
|
|
|
await message.reply_text(text)
|
|
|
|
return
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.info(f"用户: {user.full_name} [{user.id}] 使用 map 命令查询了 {resource_name}")
|
2022-07-26 16:19:21 +00:00
|
|
|
text = await self.map_helper.get_resource_map_mes(resource_name)
|
|
|
|
if "不知道" in text or "没有找到" in text:
|
|
|
|
await message.reply_text(text, parse_mode="Markdown")
|
|
|
|
return
|
|
|
|
img = Image.open(f"cache{sep}map.jpg")
|
|
|
|
if img.size[0] > 2048 or img.size[1] > 2048:
|
2022-10-10 11:07:28 +00:00
|
|
|
await message.reply_document(open(f"cache{sep}map.jpg", mode="rb+"), caption=text)
|
2022-07-26 16:19:21 +00:00
|
|
|
else:
|
2022-10-10 11:07:28 +00:00
|
|
|
await message.reply_photo(open(f"cache{sep}map.jpg", mode="rb+"), caption=text)
|