refactor: improved error handling, timeouts and use temp files

This commit is contained in:
2026-05-21 22:14:29 +02:00
parent cd0ce06246
commit 56e5d79e3e
3 changed files with 26 additions and 18 deletions
+1 -1
View File
@@ -1,8 +1,8 @@
import os
import environs
env = environs.Env()
try:
env = environs.Env()
env.read_env("./.env")
except FileNotFoundError:
print("No .env file found, using os.environ.")
+12 -12
View File
@@ -22,6 +22,7 @@ import re
import shlex
import subprocess
import sys
import tempfile
import time
import traceback
from PIL import Image
@@ -85,13 +86,17 @@ async def edit_or_send_as_file(
return
if len(tex) > 1024:
await message.edit("<code>OutPut is Too Large, Sending As File!</code>")
file_names = f"{file_name}.txt"
with open(file_names, "w") as fn:
with tempfile.NamedTemporaryFile(
"w", delete=False, suffix=".txt", prefix=f"{file_name}_"
) as fn:
fn.write(tex)
await client.send_document(message.chat.id, file_names, caption=caption)
await message.delete()
if os.path.exists(file_names):
os.remove(file_names)
temp_path = fn.name
try:
await client.send_document(message.chat.id, temp_path, caption=caption)
await message.delete()
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
return
return await message.edit(tex)
@@ -249,12 +254,7 @@ def is_admin(func):
chat_member = await client.get_chat_member(
message.chat.id, message.from_user.id
)
chat_admins = []
async for member in client.get_chat_members(
message.chat.id, filter=ChatMembersFilter.ADMINISTRATORS
):
chat_admins.append(member)
if chat_member in chat_admins:
if chat_member.status in ("administrator", "creator"):
return await func(client, message)
await message.edit("You need to be an admin to perform this action.")
except UserNotParticipant: