Files
LGBot/src/action_reporter.py

80 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from telebot.async_telebot import AsyncTeleBot
from telebot.types import Message
import logging
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 = "👤 <b>Пользователь:</b>\n"
if nickname:
text += f"• Name: <code>{nickname}</code>\n"
if tag:
text += f"• Tag: <code>@{tag}</code>\n"
text += f"• ID: <code>{user_id}</code>"
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 = "🛡 <b>Администратор:</b>\n"
if nickname:
text += f"• Name: <code>{nickname}</code>\n"
if tag:
text += f"• Tag: <code>@{tag}</code>\n"
text += f"• ID: <code>{admin_id}</code>"
return text
# Отправляет лог действия в админ-чат
async def log_action(self, action: str, user_id: int, admin_id: int, reason: str, duration: str):
try:
# Получаем информацию о пользователе и администраторе
user_info = await self._get_user_info(user_id)
admin_info = await self._get_admin_info(admin_id)
# Формируем сообщение
msg = f"⚙️ <b>Действие:</b> {action}\n"
if duration:
msg += f"⏱ <b>Длительность:</b> {duration}\n"
if reason:
msg += f"📝 <b>Причина:</b> <i>{reason}</i>\n"
msg += f"\n{user_info}\n\n{admin_info}"
# Отправляем сообщение
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)