This commit is contained in:
yasirarism 2023-07-04 10:10:11 +07:00 committed by GitHub
parent 0710e99cce
commit 647ba465b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 7 deletions

View file

@ -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
Decorators.on_cmd = command

View file

@ -1 +1,2 @@
from .utils import PyromodConfig, patch, patchable
from .handle_error import handle_error

View file

@ -0,0 +1,82 @@
# tgEasy - Easy for a brighter Shine. A monkey pather add-on for Pyrogram
# Copyright (C) 2021 - 2022 Jayant Hegde Kageri <https://github.com/jayantkageri>
# 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 <http://www.gnu.org/licenses/>.
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

View file

@ -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")
await dices.reply_msg(strings("result").format(number=dices.dice.value), quote=True)