Skip to content

Commit 36312ce

Browse files
committed
fix(logs): simplify log file retrieval to avoid some errors
1 parent 0a3bb66 commit 36312ce

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

.github/copilot-instructions.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
We are building a Python project using Hydrogram (a Pyrogram fork) for Telegram bot development. When generating or suggesting code, follow these guidelines:
44

5-
1. Use Python 3.13 features appropriately:
5+
1. Use latest Python features appropriately:
66
- Type hints with the latest typing module features
7-
- Pattern matching with match/case for handling different message types
7+
- Pattern matching and structural pattern matching
88
- F-strings for all string formatting
99
- Walrus operator (:=) when it improves readability
10-
- Use structural pattern matching for command handling
10+
- And all other latest Python features
1111

1212
2. Follow PEP 8 style guidelines:
1313
- 4 spaces for indentation
@@ -16,15 +16,18 @@ We are building a Python project using Hydrogram (a Pyrogram fork) for Telegram
1616
- PascalCase for classes
1717
- UPPER_CASE for constants
1818
- Use positional arguments for logging statements instead of f-strings
19+
- Code style consistency using Ruff.
1920

2021
3. Project architecture:
2122
- Keep functions small and focused on a single responsibility
2223
- Use type hints consistently on all functions and methods
23-
- Prefer composition over inheritance
24+
- Prefer nheritance over composition when appropriate
2425
- Use async/await for all I/O operations
26+
- Use context managers for file and resource management
27+
- Use async context managers for async I/O operations
2528

2629
4. Code organization:
27-
- Organize related functionality into separate modules
30+
- Organize code into modules and packages
2831
- Use relative imports within the package
2932
- Follow a consistent naming pattern for handler functions
3033

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2025 Hitalo M. <https://github.com/HitaloM>
33

4-
import operator
4+
import contextlib
55
from datetime import UTC, datetime
6+
from io import BytesIO
67

8+
import aiofiles
79
from hydrogram import Client
10+
from hydrogram.errors import MessageDeleteForbidden
811
from hydrogram.types import Message
912

1013
from korone.decorators import router
@@ -14,30 +17,38 @@
1417

1518
@router.message(Command("logs", disableable=False) & IsSudo)
1619
async def handle_logs_command(client: Client, message: Message) -> None:
17-
if not (log_files := list(LOG_DIR.glob("korone.log*"))):
20+
log_files = list(LOG_DIR.glob("korone.log*"))
21+
if not log_files:
1822
await message.reply("No log files found.")
1923
return
2024

21-
file_times = {file: file.stat().st_mtime for file in log_files}
22-
match max(file_times.items(), key=operator.itemgetter(1)):
23-
case (most_recent_file, mtime):
24-
file_size = round(most_recent_file.stat().st_size / (1024 * 1024), 2)
25-
timestamp = datetime.fromtimestamp(mtime, UTC).strftime("%Y-%m-%d %H:%M:%S")
26-
caption = (
27-
f"<b>📄 Log File</b>\n"
28-
f"<b>File:</b> <code>{most_recent_file.name}</code>\n"
29-
f"<b>Size:</b> <code>{file_size} MB</code>\n"
30-
f"<b>Last Modified:</b> <code>{timestamp} UTC</code>"
31-
)
25+
most_recent_file = max(log_files, key=lambda f: f.stat().st_mtime)
26+
file_size = round(most_recent_file.stat().st_size / (1024 * 1024), 2)
27+
timestamp = datetime.fromtimestamp(most_recent_file.stat().st_mtime, UTC).strftime(
28+
"%Y-%m-%d %H:%M:%S"
29+
)
30+
caption = (
31+
f"<b>📄 Log File</b>\n"
32+
f"<b>File:</b> <code>{most_recent_file.name}</code>\n"
33+
f"<b>Size:</b> <code>{file_size} MB</code>\n"
34+
f"<b>Last Modified:</b> <code>{timestamp} UTC</code>"
35+
)
3236

3337
status_message = await message.reply("Uploading log file...")
3438

3539
try:
40+
async with aiofiles.open(most_recent_file, "rb") as file:
41+
file_buffer = BytesIO(await file.read())
42+
file_buffer.seek(0)
43+
3644
await message.reply_document(
37-
document=str(most_recent_file),
45+
document=file_buffer,
3846
caption=caption,
3947
file_name=most_recent_file.name,
4048
)
41-
await status_message.delete()
49+
50+
with contextlib.suppress(MessageDeleteForbidden):
51+
await status_message.delete()
52+
4253
except Exception as exc:
43-
await status_message.edit(f"Error sending log file: {exc!s}")
54+
await status_message.edit(f"Error sending log file: {exc}")

0 commit comments

Comments
 (0)