refactor: improved error handling, timeouts and use temp files
This commit is contained in:
+13
-5
@@ -90,11 +90,19 @@ def load_missing_modules():
|
|||||||
os.makedirs(custom_modules_path, exist_ok=True)
|
os.makedirs(custom_modules_path, exist_ok=True)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = requests.get(
|
resp = requests.get(
|
||||||
"https://raw.githubusercontent.com/The-MoonTg-project/custom_modules/main/full.txt"
|
"https://raw.githubusercontent.com/The-MoonTg-project/custom_modules/main/full.txt",
|
||||||
).text
|
timeout=10,
|
||||||
except Exception:
|
)
|
||||||
logging.error("Failed to fetch custom modules list")
|
if not resp.ok:
|
||||||
|
logging.error(
|
||||||
|
"Failed to fetch custom modules list: HTTP %s",
|
||||||
|
resp.status_code,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
f = resp.text
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Failed to fetch custom modules list: %s", e)
|
||||||
return
|
return
|
||||||
modules_dict = {
|
modules_dict = {
|
||||||
line.split("/")[-1].split()[0]: line.strip() for line in f.splitlines()
|
line.split("/")[-1].split()[0]: line.strip() for line in f.splitlines()
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import os
|
import os
|
||||||
import environs
|
import environs
|
||||||
|
|
||||||
|
env = environs.Env()
|
||||||
try:
|
try:
|
||||||
env = environs.Env()
|
|
||||||
env.read_env("./.env")
|
env.read_env("./.env")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("No .env file found, using os.environ.")
|
print("No .env file found, using os.environ.")
|
||||||
|
|||||||
+12
-12
@@ -22,6 +22,7 @@ import re
|
|||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
@@ -85,13 +86,17 @@ async def edit_or_send_as_file(
|
|||||||
return
|
return
|
||||||
if len(tex) > 1024:
|
if len(tex) > 1024:
|
||||||
await message.edit("<code>OutPut is Too Large, Sending As File!</code>")
|
await message.edit("<code>OutPut is Too Large, Sending As File!</code>")
|
||||||
file_names = f"{file_name}.txt"
|
with tempfile.NamedTemporaryFile(
|
||||||
with open(file_names, "w") as fn:
|
"w", delete=False, suffix=".txt", prefix=f"{file_name}_"
|
||||||
|
) as fn:
|
||||||
fn.write(tex)
|
fn.write(tex)
|
||||||
await client.send_document(message.chat.id, file_names, caption=caption)
|
temp_path = fn.name
|
||||||
await message.delete()
|
try:
|
||||||
if os.path.exists(file_names):
|
await client.send_document(message.chat.id, temp_path, caption=caption)
|
||||||
os.remove(file_names)
|
await message.delete()
|
||||||
|
finally:
|
||||||
|
if os.path.exists(temp_path):
|
||||||
|
os.remove(temp_path)
|
||||||
return
|
return
|
||||||
return await message.edit(tex)
|
return await message.edit(tex)
|
||||||
|
|
||||||
@@ -249,12 +254,7 @@ def is_admin(func):
|
|||||||
chat_member = await client.get_chat_member(
|
chat_member = await client.get_chat_member(
|
||||||
message.chat.id, message.from_user.id
|
message.chat.id, message.from_user.id
|
||||||
)
|
)
|
||||||
chat_admins = []
|
if chat_member.status in ("administrator", "creator"):
|
||||||
async for member in client.get_chat_members(
|
|
||||||
message.chat.id, filter=ChatMembersFilter.ADMINISTRATORS
|
|
||||||
):
|
|
||||||
chat_admins.append(member)
|
|
||||||
if chat_member in chat_admins:
|
|
||||||
return await func(client, message)
|
return await func(client, message)
|
||||||
await message.edit("You need to be an admin to perform this action.")
|
await message.edit("You need to be an admin to perform this action.")
|
||||||
except UserNotParticipant:
|
except UserNotParticipant:
|
||||||
|
|||||||
Reference in New Issue
Block a user