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 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()

View File

@ -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 <cmd|None>"""
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')