from telebot.async_telebot import AsyncTeleBot
from telebot.types import Message
import logging
import os
from database import db
# Инициализация
class ActionReporter:
def __init__(self, bot: AsyncTeleBot, log_chat_id: int, log_thread_id: int):
self.bot = bot
self.log_chat_id = log_chat_id
self.log_thread_id = log_thread_id
# Получает информацию о пользователе из базы данных
async def _get_user_info(self, user_id: int) -> str:
user_info = db.get_user(user_id)
if user_info:
# Вытаскиваем данные с кортежа
_, nickname, tag = user_info
# Формируем справку о пользователе
text = "👤 Пользователь:\n"
if nickname:
text += f"• Name: {nickname}
\n"
if tag:
text += f"• Tag: @{tag}
\n"
text += f"• ID: {user_id}
"
return text
# Получает информацию об администраторе
async def _get_admin_info(self, admin_id: int) -> str:
admin_info = db.get_user(admin_id)
if admin_info:
# Вытаскиваем данные с кортежа
_, nickname, tag = admin_info
# Формируем справку об администраторе
text = "🛡 Администратор:\n"
if nickname:
text += f"• Name: {nickname}
\n"
if tag:
text += f"• Tag: @{tag}
\n"
text += f"• ID: {admin_id}
"
return text
# Отправляет лог действия в админ-чат
async def log_action(self, action: str, user_id: int, admin_id: int, reason: str, duration: str, photo_path: str):
try:
# Получаем информацию о пользователе и администраторе
user_info = await self._get_user_info(user_id)
admin_info = await self._get_admin_info(admin_id)
# Формируем сообщение
msg = f"⚙️ Действие: {action}\n"
if duration:
msg += f"⏱ Длительность: {duration}\n"
if reason:
msg += f"📝 Причина: {reason}\n"
msg += f"\n{user_info}\n\n{admin_info}"
# Отправляем лог с изображением
if photo_path and os.path.exists(photo_path):
with open(photo_path, 'rb') as photo_file:
await self.bot.send_photo(self.log_chat_id, photo_file, caption=msg, message_thread_id=self.log_thread_id)
# Отправляем лог без изображения
else:
await self.bot.send_message(self.log_chat_id, msg, message_thread_id=self.log_thread_id)
except Exception as e:
logging.error(f"Ошибка отправки лога: {str(e)}")
# Создаем глобальный экземпляр для импорта
action_reporter = None
# Инициализирует логгер модерации
def init_action_reporter(bot: AsyncTeleBot, log_chat_id: int, log_thread_id: int):
global action_reporter
action_reporter = ActionReporter(bot, log_chat_id, log_thread_id)