errors.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """ WRITTEN BY @pokurt, https://github.com/pokurt"""
  2. import traceback
  3. from functools import wraps
  4. from pyrogram.errors.exceptions.forbidden_403 import ChatWriteForbidden
  5. from wbb import LOG_GROUP_ID, app
  6. def split_limits(text):
  7. if len(text) < 2048:
  8. return [text]
  9. lines = text.splitlines(True)
  10. small_msg = ""
  11. result = []
  12. for line in lines:
  13. if len(small_msg) + len(line) < 2048:
  14. small_msg += line
  15. else:
  16. result.append(small_msg)
  17. small_msg = line
  18. else:
  19. result.append(small_msg)
  20. return result
  21. def capture_err(func):
  22. @wraps(func)
  23. async def capture(client, message, *args, **kwargs):
  24. try:
  25. return await func(client, message, *args, **kwargs)
  26. except ChatWriteForbidden:
  27. await app.leave_chat(message.chat.id)
  28. return
  29. except Exception as err:
  30. errors = traceback.format_exc()
  31. error_feedback = split_limits(
  32. "**ERROR** | `{}` | `{}`\n\n```{}```\n\n```{}```\n".format(
  33. 0 if not message.from_user else message.from_user.id,
  34. 0 if not message.chat else message.chat.id,
  35. message.text or message.caption,
  36. "".join(errors),
  37. ),
  38. )
  39. for x in error_feedback:
  40. await app.send_message(LOG_GROUP_ID, x)
  41. raise err
  42. return capture