From 34d5d5f70b2f3d7360b8166c08661eb089846cc0 Mon Sep 17 00:00:00 2001 From: iwumingz Date: Sat, 9 Apr 2022 13:35:49 +0800 Subject: [PATCH] Fix some bugs --- plugins/google.py | 21 +++++++++++++-------- plugins/help.py | 20 +++++++++----------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/plugins/google.py b/plugins/google.py index 0bb2c4d..860547a 100644 --- a/plugins/google.py +++ b/plugins/google.py @@ -1,4 +1,5 @@ import asyncio +import re from core import command from loguru import logger @@ -6,24 +7,28 @@ from pyrogram import Client from pyrogram.errors import FloodWait from pyrogram.types import Message from tools.googles import google_search -from tools.helpers import Parameters +from tools.helpers import Parameters, show_cmd_tip, show_exception @Client.on_message(command("google")) async def google(_: Client, msg: Message): """谷歌搜索并展示第一页结果和链接""" + cmd, args = Parameters.get(msg) replied_msg = msg.reply_to_message - if not replied_msg: - _, args = Parameters.get(msg) + if not args and replied_msg and (replied_msg.text or replied_msg.caption): + pattern = replied_msg.text or replied_msg.caption + elif args: + pattern = args else: - args = replied_msg.text or replied_msg.caption + return await show_cmd_tip(msg, cmd) try: - res = await google_search(args) - content = '\n\n'.join( + res = await google_search(pattern) + links = '\n\n'.join( f"[{title[0:30]}]({url})" for title, url in res.items() ) - text = f"🔎 | **Google** | `{args}`\n{content}" + pattern = re.sub(r'[_*`[]', '', pattern[0:30]) + text = f"🔎 | **Google** | `{pattern}`\n{links}" await msg.edit_text( text=text, parse_mode='md', @@ -38,6 +43,6 @@ async def google(_: Client, msg: Message): ) except Exception as e: logger.error(e) - await msg.edit_text("❗️ Unable to connect to google") + await show_exception(msg, "Unable to connect to google") finally: await logger.complete() diff --git a/plugins/help.py b/plugins/help.py index 70a78d1..456f03b 100644 --- a/plugins/help.py +++ b/plugins/help.py @@ -1,10 +1,6 @@ -from typing import Any, Dict - -import yaml -from core import command +from core import CMDS_DATA, command from pyrogram import Client from pyrogram.types import Message -from tools.constants import COMMAND_YML from tools.helpers import Parameters @@ -12,14 +8,16 @@ from tools.helpers import Parameters async def helper(_: Client, msg: Message): """指令用法提示。格式:-help """ helper_cmd, cmd = Parameters.get(msg) - cmd_data: Dict[str, Any] = yaml.full_load(open(COMMAND_YML, 'rb')) + data = CMDS_DATA + cmd_alias = dict(zip((v.get('cmd') for v in data.values()), data.keys())) if not cmd: - tmp = '、'.join(f"`{k}`" for k in cmd_data.keys()) + tmp = '、'.join(f"`{k}`" for k in data.keys()) text = f"📢 **指令列表:**\n{tmp}\n\n**发送** `{helper_cmd} " \ f"<{cmd if cmd else 'cmd'}>` **查看某指令的详细用法**" - elif not cmd_data.get(cmd): - text = f'❓ `{cmd}` 404 Not Found' + elif not data.get(cmd) and cmd not in cmd_alias: + text = f"❗️ Without this command >>> `{cmd}`" else: - text = f"格式:`{cmd_data.get(cmd).get('format')}`\n" \ - f"用法:`{cmd_data.get(cmd).get('usage')}`" + key = cmd if data.get(cmd) else cmd_alias.get(cmd) + text = f"格式:`{data.get(key).get('format')}`\n" \ + f"用法:`{data.get(key).get('usage')}`" await msg.edit_text(text, parse_mode='md')