diff --git a/misskaty/core/misskaty_patch/decorators/command.py b/misskaty/core/misskaty_patch/decorators/command.py index c1c9ecc1..77ead0f4 100644 --- a/misskaty/core/misskaty_patch/decorators/command.py +++ b/misskaty/core/misskaty_patch/decorators/command.py @@ -1,5 +1,6 @@ import typing import pyrogram +from .utils import handle_error from pyrogram.methods import Decorators from misskaty.vars import COMMAND_HANDLER @@ -111,7 +112,7 @@ def command( except pyrogram.errors.exceptions.forbidden_403.ChatWriteForbidden: await client.leave_chat(message.chat.id) except BaseException as exception: - return + return await handle_error(exception, message) self.add_handler( pyrogram.handlers.MessageHandler(callback=decorator, filters=filter) @@ -120,4 +121,4 @@ def command( return wrapper -Decorators.command = command \ No newline at end of file +Decorators.on_cmd = command \ No newline at end of file diff --git a/misskaty/core/misskaty_patch/utils/__init__.py b/misskaty/core/misskaty_patch/utils/__init__.py index a788cb66..5f451464 100644 --- a/misskaty/core/misskaty_patch/utils/__init__.py +++ b/misskaty/core/misskaty_patch/utils/__init__.py @@ -1 +1,2 @@ from .utils import PyromodConfig, patch, patchable +from .handle_error import handle_error \ No newline at end of file diff --git a/misskaty/core/misskaty_patch/utils/handler_error.py b/misskaty/core/misskaty_patch/utils/handler_error.py new file mode 100644 index 00000000..186421cc --- /dev/null +++ b/misskaty/core/misskaty_patch/utils/handler_error.py @@ -0,0 +1,82 @@ +# tgEasy - Easy for a brighter Shine. A monkey pather add-on for Pyrogram +# Copyright (C) 2021 - 2022 Jayant Hegde Kageri + +# This file is part of tgEasy. + +# tgEasy is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# tgEasy is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. + +# You should have received a copy of the GNU Lesser General Public License +# along with tgEasy. If not, see . +import contextlib +import os +import typing +import pyrogram +from misskaty.vars import LOG_CHANNEL + + +async def handle_error( + error, m: typing.Union[pyrogram.types.Message, pyrogram.types.CallbackQuery] +): + """ + ### `handle_error` + - A Function to Handle the Errors in Functions. + - This Sends the Error Log to the Log Group and Replies Sorry Message for the Users. + - This is Helper for all of the functions for handling the Errors. + + - Parameters: + - error: + - The Exceptation. + + - m (`pyrogram.types.Message` or `pyrogram.types.CallbackQuery`): + - The Message or Callback Query where the Error occurred. + + #### Exapmle + .. code-block:: python + from tgEasy import tgClient, handle_error + import pyrogram + + app = tgClient(pyrogram.Client()) + + @app.command("start") + async def start(client, message): + try: + await message.reply_text("Hi :D') # I intentionally made an bug for Example :/ + except Exceptation as e: + return await handle_error(e, message) + """ + import traceback + import logging + + logging = logger.getLogger(__name__) + logging.exception(traceback.format_exc()) + + with open("crash.txt", "w+", encoding="utf-8") as log: + log.write(traceback.format_exc()) + log.close() + if isinstance(m, pyrogram.types.Message): + with contextlib.suppress(Exception): + await m.reply_text( + "An Internal Error Occurred while Processing your Command, the Logs have been sent to the Owners of this Bot. Sorry for Inconvenience" + ) + await m._client.send_document( + LOG_CHANNEL, "crash.txt", caption="Crash Report of this Bot" + ) + if isinstance(m, pyrogram.types.CallbackQuery): + with contextlib.suppress(Exception): + await m.message.delete() + await m.message.reply_text( + "An Internal Error Occurred while Processing your Command, the Logs have been sent to the Owners of this Bot. Sorry for Inconvenience" + ) + await m.message._client.send_document( + LOG_CHANNEL, "crash.txt", caption="Crash Report of this Bot" + ) + os.remove("crash.txt") + return True \ No newline at end of file diff --git a/misskaty/plugins/fun.py b/misskaty/plugins/fun.py index 51561ddc..260e3a39 100644 --- a/misskaty/plugins/fun.py +++ b/misskaty/plugins/fun.py @@ -177,8 +177,4 @@ async def memify(client, message): @use_chat_lang() async def dice(c, m, strings): dices = await c.send_dice(m.chat.id, reply_to_message_id=m.id) - await dices.reply_msg(strings("result").format(number=dices.dice.value), quote=True) - -@app.command("cekdulu") -async def tesdulu(c, m): - await m.reply("DONne") \ No newline at end of file + await dices.reply_msg(strings("result").format(number=dices.dice.value), quote=True) \ No newline at end of file