Исправлены баги с записью в файл
This commit is contained in:
@@ -5,9 +5,10 @@ import os
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from config import BotVariables
|
from ..configs import *
|
||||||
from .type_messages import *
|
from ..library.time import TimeVariable
|
||||||
from .find_ids import find_chat_id
|
from .types_msg import types_message, types_chat
|
||||||
|
from .find_ids import find_imp_id
|
||||||
|
|
||||||
# Настройка экспорта модулей и логирования
|
# Настройка экспорта модулей и логирования
|
||||||
__all__ = ("write_message_to_file",)
|
__all__ = ("write_message_to_file",)
|
||||||
@@ -19,12 +20,12 @@ async def write_message_to_file(message):
|
|||||||
try:
|
try:
|
||||||
# Создание переменных с информацией
|
# Создание переменных с информацией
|
||||||
message_type = types_message(message)
|
message_type = types_message(message)
|
||||||
chat_id = find_chat_id(message)
|
chat_id = find_imp_id(message.from_user.id)
|
||||||
file_path = types_chat(message)
|
file_path = types_chat(message)
|
||||||
|
|
||||||
# Переменные для логов сообщений
|
# Переменные для логов сообщений
|
||||||
date = datetime.now().strftime(BotVariables.time_format)
|
date = datetime.now().strftime(TimeVariable.format)
|
||||||
username = message.from_user.username if message.from_user.username else "No username"
|
username = message.from_user.username if message.from_user.username else "Нет @username"
|
||||||
log_shablon = f"{date} | @{username} ({message.from_user.id}) |"
|
log_shablon = f"{date} | @{username} ({message.from_user.id}) |"
|
||||||
|
|
||||||
# Проверка и создание директорий
|
# Проверка и создание директорий
|
||||||
@@ -32,17 +33,14 @@ async def write_message_to_file(message):
|
|||||||
os.makedirs(os.path.dirname(file_path))
|
os.makedirs(os.path.dirname(file_path))
|
||||||
|
|
||||||
# Запись информации в файл
|
# Запись информации в файл
|
||||||
with open(file_path, "a", encoding=BotVariables.encoding) as file:
|
with open(file_path, "a", encoding=default_encod) as file:
|
||||||
# Проверка на наличие текста
|
# Проверка на наличие текста
|
||||||
if message.text is None:
|
if message.text is None:
|
||||||
file.write(f"{log_shablon} Тип: {message_type}\n\n")
|
file.write(f"{log_shablon} Тип: {message_type}\n\n")
|
||||||
else:
|
else:
|
||||||
file.write(f"{log_shablon} ChatID: {chat_id} | Текст: {message.text}\n\n")
|
file.write(f"{log_shablon} ChatID: {chat_id} | Текст: {message.text}\n\n")
|
||||||
|
|
||||||
return f"Сообщение пользователя успешно записано в файл."
|
|
||||||
|
|
||||||
# Проверка на ошибку и ее логирование
|
# Проверка на ошибку и ее логирование
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
text_error = f"Ошибка в сохранении сообщения в файл: {str(e)}"
|
text_error = f"Ошибка в сохранении сообщения в файл: {str(e)}"
|
||||||
logger.bind(custom_variable=log_type, user_var=f"@{message.from_user.username}").error(text_error)
|
logger.bind(custom_variable=log_type, user_var=f"@{message.from_user.username}").error(text_error)
|
||||||
return text_error
|
|
||||||
|
|||||||
Reference in New Issue
Block a user