Fix some bugs

This commit is contained in:
iwumingz 2022-04-09 13:35:49 +08:00
parent 06a9d99c00
commit 34d5d5f70b
2 changed files with 22 additions and 19 deletions

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import re
from core import command from core import command
from loguru import logger from loguru import logger
@ -6,24 +7,28 @@ from pyrogram import Client
from pyrogram.errors import FloodWait from pyrogram.errors import FloodWait
from pyrogram.types import Message from pyrogram.types import Message
from tools.googles import google_search 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")) @Client.on_message(command("google"))
async def google(_: Client, msg: Message): async def google(_: Client, msg: Message):
"""谷歌搜索并展示第一页结果和链接""" """谷歌搜索并展示第一页结果和链接"""
cmd, args = Parameters.get(msg)
replied_msg = msg.reply_to_message replied_msg = msg.reply_to_message
if not replied_msg: if not args and replied_msg and (replied_msg.text or replied_msg.caption):
_, args = Parameters.get(msg) pattern = replied_msg.text or replied_msg.caption
elif args:
pattern = args
else: else:
args = replied_msg.text or replied_msg.caption return await show_cmd_tip(msg, cmd)
try: try:
res = await google_search(args) res = await google_search(pattern)
content = '\n\n'.join( links = '\n\n'.join(
f"[{title[0:30]}]({url})" for title, url in res.items() 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( await msg.edit_text(
text=text, text=text,
parse_mode='md', parse_mode='md',
@ -38,6 +43,6 @@ async def google(_: Client, msg: Message):
) )
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
await msg.edit_text("❗️ Unable to connect to google") await show_exception(msg, "Unable to connect to google")
finally: finally:
await logger.complete() await logger.complete()

View File

@ -1,10 +1,6 @@
from typing import Any, Dict from core import CMDS_DATA, command
import yaml
from core import command
from pyrogram import Client from pyrogram import Client
from pyrogram.types import Message from pyrogram.types import Message
from tools.constants import COMMAND_YML
from tools.helpers import Parameters from tools.helpers import Parameters
@ -12,14 +8,16 @@ from tools.helpers import Parameters
async def helper(_: Client, msg: Message): async def helper(_: Client, msg: Message):
"""指令用法提示。格式:-help <cmd|None>""" """指令用法提示。格式:-help <cmd|None>"""
helper_cmd, cmd = Parameters.get(msg) 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: 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} " \ text = f"📢 **指令列表:**\n{tmp}\n\n**发送** `{helper_cmd} " \
f"<{cmd if cmd else 'cmd'}>` **查看某指令的详细用法**" f"<{cmd if cmd else 'cmd'}>` **查看某指令的详细用法**"
elif not cmd_data.get(cmd): elif not data.get(cmd) and cmd not in cmd_alias:
text = f'❓ `{cmd}` 404 Not Found' text = f"❗️ Without this command >>> `{cmd}`"
else: else:
text = f"格式:`{cmd_data.get(cmd).get('format')}`\n" \ key = cmd if data.get(cmd) else cmd_alias.get(cmd)
f"用法:`{cmd_data.get(cmd).get('usage')}`" text = f"格式:`{data.get(key).get('format')}`\n" \
f"用法:`{data.get(key).get('usage')}`"
await msg.edit_text(text, parse_mode='md') await msg.edit_text(text, parse_mode='md')