From c6982b5e9b2445388ff2345be5735814c877f505 Mon Sep 17 00:00:00 2001 From: iwumingz Date: Sat, 9 Apr 2022 13:11:35 +0800 Subject: [PATCH] Modify error message --- plugins/calculate.py | 5 ++--- plugins/cc.py | 5 ++--- plugins/shell.py | 14 ++++++-------- plugins/speedtest.py | 29 +++++++++++++++-------------- plugins/sticker.py | 15 +++++++-------- tools/helpers.py | 18 +++++++++++------- 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/plugins/calculate.py b/plugins/calculate.py index b10226c..9b6ebbf 100644 --- a/plugins/calculate.py +++ b/plugins/calculate.py @@ -4,7 +4,7 @@ from core import command from pyrogram import Client from pyrogram.errors import FloodWait, RPCError from pyrogram.types import Message -from tools.helpers import Parameters, basher +from tools.helpers import Parameters, basher, show_exception @Client.on_message(command("cal")) @@ -14,8 +14,7 @@ async def calculate(_: Client, msg: Message): try: res = await basher(f"""echo "scale=4;{args}" | bc""", 3) except asyncio.exceptions.TimeoutError: - await msg.edit_text("❗️ Connection Timeout") - return + return await show_exception(msg, "Connection Timeout!") if not res.get('output'): await msg.edit_text(f"Error:{res.get('error')}") return diff --git a/plugins/cc.py b/plugins/cc.py index 2fd5b9e..bef82f4 100644 --- a/plugins/cc.py +++ b/plugins/cc.py @@ -9,7 +9,7 @@ from pyrogram import Client from pyrogram.errors import FloodWait from pyrogram.types import Message from tools.constants import CC_MAX_TIMES, REACTIONS, STORE_CC_DATA, TG_GROUPS -from tools.helpers import Parameters, delete_this, emoji_sender, get_cmd_error +from tools.helpers import Parameters, delete_this, emoji_sender, show_cmd_tip from tools.storage import SimpleStore @@ -40,8 +40,7 @@ async def cc(cli: Client, msg: Message): await msg.edit_text(f"Default emoji changed to `{tmp}`") return else: - await msg.edit_text(get_cmd_error(cmd)) - return + return await show_cmd_tip(msg, cmd) # 攻击次数 cc_times = cc_times if 1 <= cc_times <= CC_MAX_TIMES else CC_MAX_TIMES diff --git a/plugins/shell.py b/plugins/shell.py index 690a99f..1c14d78 100644 --- a/plugins/shell.py +++ b/plugins/shell.py @@ -6,7 +6,7 @@ from platform import node from core import command from pyrogram import Client from pyrogram.types import Message -from tools.helpers import Parameters, basher, delete_this, get_cmd_error +from tools.helpers import Parameters, basher, delete_this, show_cmd_tip, show_exception @Client.on_message(command("sh")) @@ -14,27 +14,25 @@ async def shell(_: Client, msg: Message): """执行shell脚本""" cmd, _input = Parameters.get(msg) if not _input: - await msg.edit_text(get_cmd_error(cmd)) - return + return await show_cmd_tip(msg, cmd) try: res = await basher(_input, timeout=30) except asyncio.exceptions.TimeoutError: - await msg.edit_text('❗️ Connection Timeout') - return + return await show_exception(msg, "Connection Timeout!") - _output: str = res.get('output') if not res.get('error') else res.get('error') + _output: str = res.get('output') if not res.get( + 'error') else res.get('error') header = f"**{getuser()}@{node()}**\n" all_bytes = len(header.encode() + _input.encode() + _output.encode()) if all_bytes >= 2048: await delete_this(msg) - await msg.reply_document( + return await msg.reply_document( document=BytesIO(_output.encode()), caption=f"{header}> # `{_input}`", file_name="output.log", parse_mode='md' ) - return await msg.edit_text( f"{header}> # `{_input}`\n```{_output.strip()}```", diff --git a/plugins/speedtest.py b/plugins/speedtest.py index 91fb1f8..d49572a 100644 --- a/plugins/speedtest.py +++ b/plugins/speedtest.py @@ -5,8 +5,8 @@ from core import command from pyrogram import Client from pyrogram.errors import FloodWait from pyrogram.types import Message -from tools.constants import SPEEDTEST_RUN -from tools.helpers import Parameters, delete_this, get_cmd_error, show_error +from tools.constants import SPEEDTEST_RUN, SYCGRAM_INFO +from tools.helpers import Parameters, delete_this, show_cmd_tip, show_exception from tools.speedtests import Speedtester @@ -21,19 +21,23 @@ async def speedtest(_: Client, msg: Message): try: update_res = await tester.init_for_speedtest('update') except asyncio.exceptions.TimeoutError: - await msg.edit_text("⚠️ Update Timeout") + await show_exception(msg, "Update Timeout!") except Exception as e: - await show_error(msg, e) + await show_exception(msg, e) else: - # TODO:有个未知错误 - await msg.edit_text(f"Result\n```{update_res}```") + await msg.edit_text( + f"**{SYCGRAM_INFO}**\n> # `{update_res}`", + parse_mode='md' + ) return elif opt == 'list': try: text = await tester.list_servers_ids(f"{SPEEDTEST_RUN} -L") await msg.edit_text(text, parse_mode='md') except asyncio.exceptions.TimeoutError: - await msg.edit_text("⚠️ Speedtest Timeout") + await show_exception(msg, "Speedtest Timeout") + except Exception as e: + await show_exception(msg, e) return elif bool(re.match(r'[0-9]+', opt)) or not opt: try: @@ -41,15 +45,12 @@ async def speedtest(_: Client, msg: Message): f"""{SPEEDTEST_RUN}{'' if not opt else f' -s {opt}'}""" ) except asyncio.exceptions.TimeoutError: - await msg.edit_text("⚠️ Speedtest Timeout") - return + return await show_exception(msg, "Speedtest Timeout") else: - await msg.edit_text(get_cmd_error(cmd)) - return + return await show_cmd_tip(msg, cmd) if not link: - await msg.edit_text(text) - return + return await show_exception(msg, "Speedtest Connection Error") # send speed report try: @@ -58,6 +59,6 @@ async def speedtest(_: Client, msg: Message): await asyncio.sleep(e.x) await msg.reply_photo(photo=link, caption=text, parse_mode='md') except Exception as e: - await show_error(msg, e) + await show_exception(msg, e) # delete cmd history await delete_this(msg) diff --git a/plugins/sticker.py b/plugins/sticker.py index cfb7f51..a022a2f 100644 --- a/plugins/sticker.py +++ b/plugins/sticker.py @@ -6,7 +6,8 @@ from loguru import logger from pyrogram import Client, filters from pyrogram.types import Message from tools.constants import STICKER_BOT, STICKER_ERROR_LIST -from tools.helpers import Parameters, check_if_package_existed, get_default_pkg +from tools.helpers import (Parameters, check_if_package_existed, + get_default_pkg, show_exception) from tools.stickers import StickerAdder, sticker_cond, sticker_locker from tools.storage import SimpleStore @@ -40,7 +41,7 @@ async def sticker(cli: Client, msg: Message): # 处理参数 if len(args) != 2: pkg_title, pkg_name = get_default_pkg(msg.from_user) - await msg.edit('✅ Reset sticker title and name to default..') + await msg.edit_text('✅ Reset sticker title and name to default..') else: pkg_title, pkg_name = args if len(pkg_title.encode()) >= 168: @@ -49,22 +50,21 @@ async def sticker(cli: Client, msg: Message): elif len(pkg_title.encode()) >= 58: await msg.edit_text('❗️ Too long sticker set name.') return - await msg.edit('✅ Customize sticker title and name successfully.') + await msg.edit_text('✅ Customize sticker title and name successfully.') async with SimpleStore() as store: store.data['sticker_set_title'] = pkg_title store.data['sticker_set_name'] = pkg_name return - async with SimpleStore() as store: + async with SimpleStore(auto_flush=False) as store: pkg_title = store.data.get('sticker_set_title') pkg_name = store.data.get('sticker_set_name') if not pkg_title or not pkg_name: - await msg.edit_text( + return await msg.edit_text( "⚠️ The default sticker title and name are empty, " "please use `-s` reset!" ) - return # 尝试检查贴纸包是否存在 try: @@ -72,8 +72,7 @@ async def sticker(cli: Client, msg: Message): except Exception as e: # 无法判定是否贴纸包存在 logger.error(e) - await msg.edit_text('⚠️ Network Error | Please try again later ...') - return + return await show_exception(msg, e) # 开始前的检查 await msg.edit_text('👆 Working on adding stickers ...') diff --git a/tools/helpers.py b/tools/helpers.py index 7d74e9a..12518f7 100644 --- a/tools/helpers.py +++ b/tools/helpers.py @@ -3,12 +3,13 @@ import re from typing import Any, Dict, List, Tuple, Union from bs4 import BeautifulSoup +from core.custom import CMDS_PREFIX from loguru import logger from pyrogram import Client from pyrogram.errors import FloodWait, RPCError from pyrogram.types import Message, User -from .constants import STICKER_DESCRIP +from .constants import STICKER_DESCRIP, SYCGRAM_ERROR, SYCGRAM_INFO from .sessions import session @@ -86,8 +87,15 @@ def is_deleted_id(msg: Message) -> bool: return bool(msg.message_id > 1 and msg.from_user and msg.from_user.is_self) -def get_cmd_error(cmd: str) -> str: - return f"Use `-help {cmd.replace('-', '', 1)}` to view detailed usage." +async def show_exception(msg: Message, e: Any) -> None: + text = f"**{SYCGRAM_ERROR}**\n> # `{e}`" + await msg.edit_text(text, parse_mode='md') + + +async def show_cmd_tip(msg: Message, cmd: str) -> str: + tip = f"Use `{CMDS_PREFIX}help {cmd.replace(CMDS_PREFIX, '', 1)}` to view detailed usage." + text = f"**{SYCGRAM_INFO}**\n> # {tip}" + await msg.edit_text(text, parse_mode='md') async def check_if_package_existed(pkg_name: str) -> bool: @@ -161,10 +169,6 @@ async def execute(command: str) -> Dict[str, Any]: } -async def show_error(msg: Message, e: Any) -> None: - await msg.edit_text(f"⚠️ Error\n```{e}```") - - async def kick_one(cli: Client, cid: Union[int, str], uid: Union[int, str]): me = await cli.get_chat_member(cid, 'me') if me.can_restrict_members and await cli.ban_chat_member(cid, uid):