mute and ban commands with photos
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
from telebot.async_telebot import AsyncTeleBot
|
||||
from telebot.types import Message
|
||||
import logging
|
||||
import os
|
||||
from database import db
|
||||
|
||||
# Инициализация
|
||||
@ -47,7 +48,7 @@ class ActionReporter:
|
||||
return text
|
||||
|
||||
# Отправляет лог действия в админ-чат
|
||||
async def log_action(self, action: str, user_id: int, admin_id: int, reason: str, duration: str):
|
||||
async def log_action(self, action: str, user_id: int, admin_id: int, reason: str, duration: str, photo_path: str):
|
||||
try:
|
||||
|
||||
# Получаем информацию о пользователе и администраторе
|
||||
@ -65,7 +66,13 @@ class ActionReporter:
|
||||
|
||||
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:
|
||||
|
@ -26,10 +26,16 @@ async def delete_messages(bot: AsyncTeleBot, message: Message, time_sleep: int):
|
||||
await bot.delete_message(message.chat.id, message.message_id)
|
||||
await bot.delete_message(message.chat.id, message.message_id+1)
|
||||
|
||||
def register_handlers(bot: AsyncTeleBot): # Регистрирует все обработчики команд
|
||||
# Регистрирует все обработчики команд
|
||||
def register_handlers(bot: AsyncTeleBot):
|
||||
|
||||
@bot.message_handler(commands=['ban']) # Обработчик команды /ban
|
||||
async def ban_command(message: Message):
|
||||
# Обработчик команды /ban
|
||||
@bot.message_handler(commands=['ban'])
|
||||
async def _ban_command_wrapper(message: Message):
|
||||
await ban_command(bot, message)
|
||||
|
||||
# Основная функция команды /ban
|
||||
async def ban_command(bot: AsyncTeleBot, message: Message, photo_path: str = None):
|
||||
|
||||
# Отправка сообщения в тему или обычный чат
|
||||
send_message = bot.reply_to if message.is_topic_message else bot.send_message
|
||||
@ -219,7 +225,8 @@ def register_handlers(bot: AsyncTeleBot): # Регистрирует все об
|
||||
user_id=target_user.id,
|
||||
admin_id=message.from_user.id,
|
||||
reason=reason,
|
||||
duration=None
|
||||
duration=None,
|
||||
photo_path=photo_path
|
||||
)
|
||||
|
||||
# Отправляем сообщения, что пользователь получил бан
|
||||
|
70
src/modules/content_filter.py
Normal file
70
src/modules/content_filter.py
Normal file
@ -0,0 +1,70 @@
|
||||
from telebot.async_telebot import AsyncTeleBot
|
||||
from telebot.types import Message
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
import logging
|
||||
from database import db
|
||||
|
||||
# Импортируем обработчики команд
|
||||
from modules.mute import mute_command
|
||||
from modules.ban import ban_command
|
||||
|
||||
logger = logging.getLogger(__name__) # Получаем логгер для текущего модуля
|
||||
|
||||
def register_handlers(bot: AsyncTeleBot): # Регистрирует все обработчики событий
|
||||
|
||||
# Обработчик изображений
|
||||
@bot.message_handler(content_types=['photo'])
|
||||
async def message_photo(message: Message):
|
||||
|
||||
# Определяем путь к изображению
|
||||
photo_path = None
|
||||
|
||||
try:
|
||||
|
||||
# Проверяем, есть ли подпись
|
||||
if not message.caption:
|
||||
return
|
||||
|
||||
# Разделяем подпись на части
|
||||
parts = message.caption.split()
|
||||
|
||||
# Определяем команду (первое слово в подписи)
|
||||
command = parts[0].lower()
|
||||
|
||||
# Поддерживаемые команды
|
||||
supported_commands = {
|
||||
'/mute': mute_command,
|
||||
'/ban': ban_command
|
||||
}
|
||||
|
||||
# Проверяем, является ли первое слово командой
|
||||
if command not in supported_commands:
|
||||
return
|
||||
|
||||
# Скачиваем изображение
|
||||
file_info = await bot.get_file(message.photo[-1].file_id)
|
||||
os.makedirs("tmp", exist_ok=True)
|
||||
photo_path = f"tmp/{file_info.file_id}.jpg"
|
||||
downloaded_file = await bot.download_file(file_info.file_path)
|
||||
|
||||
with open(photo_path, 'wb') as new_file:
|
||||
new_file.write(downloaded_file)
|
||||
|
||||
# Переносим caption в text
|
||||
message.text = message.caption
|
||||
|
||||
# Вызываем обработчик команды
|
||||
handler = supported_commands[command]
|
||||
await handler(bot, message, photo_path=photo_path)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка обработки команды с изображением: {str(e)}")
|
||||
|
||||
finally:
|
||||
if photo_path and os.path.exists(photo_path):
|
||||
try:
|
||||
os.remove(photo_path)
|
||||
except Exception as e:
|
||||
logger.error(f"Ошибка удаления временного изображения: {str(e)}")
|
@ -91,10 +91,16 @@ async def delete_messages(bot: AsyncTeleBot, message: Message, time_sleep: int):
|
||||
await bot.delete_message(message.chat.id, message.message_id)
|
||||
await bot.delete_message(message.chat.id, message.message_id+1)
|
||||
|
||||
def register_handlers(bot: AsyncTeleBot): # Регистрирует все обработчики команд
|
||||
# Регистрирует все обработчики команд
|
||||
def register_handlers(bot: AsyncTeleBot):
|
||||
|
||||
@bot.message_handler(commands=['mute']) # Обработчик команды /mute
|
||||
async def mute_command(message: Message):
|
||||
# Обработчик команды /mute
|
||||
@bot.message_handler(commands=['mute'])
|
||||
async def _mute_command_wrapper(message: Message):
|
||||
await mute_command(bot, message)
|
||||
|
||||
# Основная функция команды /mute
|
||||
async def mute_command(bot: AsyncTeleBot, message: Message, photo_path: str = None):
|
||||
|
||||
# Отправка сообщения в тему или обычный чат
|
||||
send_message = bot.reply_to if message.is_topic_message else bot.send_message
|
||||
@ -350,7 +356,8 @@ def register_handlers(bot: AsyncTeleBot): # Регистрирует все об
|
||||
user_id=target_user.id,
|
||||
admin_id=message.from_user.id,
|
||||
reason=reason,
|
||||
duration=time_display
|
||||
duration=time_display,
|
||||
photo_path=photo_path
|
||||
)
|
||||
|
||||
# Отправляем сообщения, что пользователь получил мут
|
||||
|
@ -152,7 +152,8 @@ def register_handlers(bot: AsyncTeleBot): # Регистрирует все об
|
||||
user_id=target_user.id,
|
||||
admin_id=message.from_user.id,
|
||||
reason=None,
|
||||
duration=None
|
||||
duration=None,
|
||||
photo_path=None
|
||||
)
|
||||
|
||||
# Отправляем сообщения, что пользователь получил разбан
|
||||
|
@ -168,7 +168,8 @@ def register_handlers(bot: AsyncTeleBot): # Регистрирует все об
|
||||
user_id=target_user.id,
|
||||
admin_id=message.from_user.id,
|
||||
reason=None,
|
||||
duration=None
|
||||
duration=None,
|
||||
photo_path=None
|
||||
)
|
||||
|
||||
# Отправляем сообщения, что пользователь получил размут
|
||||
|
Reference in New Issue
Block a user