Skip to content

Commit 27bb8f3

Browse files
authored
Create errors.py
1 parent f28a3df commit 27bb8f3

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Anime_Inline/helper/errors.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from functools import wraps
2+
import pyrogram
3+
from Anime_Inline import logging
4+
import traceback
5+
import sys
6+
from Anime_Inline.Cutiepii_Robot.inline import ALLOWED_USERS, LOG_CHANNEL
7+
8+
9+
def split_limits(text):
10+
if len(text) < 2048:
11+
return [text]
12+
13+
lines = text.splitlines(True)
14+
small_msg = ""
15+
result = []
16+
for line in lines:
17+
if len(small_msg) + len(line) < 2048:
18+
small_msg += line
19+
else:
20+
result.append(small_msg)
21+
small_msg = line
22+
else:
23+
result.append(small_msg)
24+
25+
return result
26+
27+
28+
def capture_err(func):
29+
@wraps(func)
30+
async def capture(client, message, *args, **kwargs):
31+
try:
32+
return await func(client, message, *args, **kwargs)
33+
except pyrogram.errors.exceptions.forbidden_403.ChatWriteForbidden as err:
34+
logging.info(
35+
"Bot was muted in {} {}".format(message.chat.title, message.chat.id)
36+
)
37+
await client.leave_chat(message.chat.id)
38+
except Exception as err:
39+
await message.reply("**Error:**\n" + str(err))
40+
exc_type, exc_obj, exc_tb = sys.exc_info()
41+
errors = traceback.format_exception(
42+
etype=exc_type, value=exc_obj, tb=exc_tb
43+
)
44+
error_feedback = split_limits(
45+
"**Anime ERROR** | `{}` | `{}`\n\n```{}```\n\n```{}```\n".format(
46+
0 if not message.from_user else message.from_user.id,
47+
0 if not message.chat else message.chat.id,
48+
message.text or message.caption,
49+
"".join(errors),
50+
)
51+
)
52+
for x in error_feedback:
53+
if LOG_CHANNEL:
54+
await client.send_message(LOG_CHANNEL, x)
55+
else:
56+
for m in ALLOWED_USERS:
57+
await client.send_message(m, x)
58+
raise err
59+
return capture

0 commit comments

Comments
 (0)