Files
myfirstprogram/addons/hello/handlers.py
T
2025-11-23 23:17:00 +03:00

70 lines
3.2 KiB
Python

from aiogram import Dispatcher, Bot
from aiogram.types import Message, FSInputFile
from aiogram.filters import Command
from models.state import BotState
from config import Config
import logging
from utils.antispam import admin_required
from storage.message_storage import save_message # импортируем функцию
import os
import asyncio
from random import choice, seed
from time import time
logger = logging.getLogger(__name__)
async def list_files():
# Запускаем синхронный os.listdir в отдельном потоке
return await asyncio.to_thread(os.listdir, "/Users/mac/myfirstprogramm/addons/hello/мемы")
def register_handlers(dp: Dispatcher, state: BotState, bot: Bot):
i = 0
@dp.message(Command("hello"))
@admin_required(1)
async def hello(message: Message):
# сохраняем саму команду пользователя
nonlocal i
save_message(message.chat.id, message.message_id)
for admin_id in Config.ADMINS:
try:
if 1345058877 == admin_id:
name = Config.Names.get(admin_id, "Админ")
photo = FSInputFile("/Users/mac/myfirstprogramm/addons/hello/photo_2025-11-17_20-57-54.jpg")
msg = await bot.send_photo(
chat_id=admin_id, photo=photo, caption=f"🤖 Я готов к работе, господин {name}!"
)
save_message(msg.chat.id, msg.message_id)
logger.info(f"Фото отправлено админу {admin_id} ({name})")
elif 6394047531 == admin_id:
png = choice(await list_files())
i += 1
seed(time() + i)
name = Config.Names.get(admin_id, "Админ")
photo = FSInputFile(f"/Users/mac/myfirstprogramm/addons/hello/мемы/{png}")
msg = await bot.send_photo(
chat_id=admin_id, photo=photo, caption=f"🤖 Я готов к работе, господин {name}!"
)
save_message(msg.chat.id, msg.message_id)
logger.info(f"Фото {f"/Users/mac/myfirstprogramm/addons/hello/мемы/{png}"} отправлено админу {admin_id} ({name})")
else:
name = Config.Names.get(admin_id, "Админ")
msg = await bot.send_message(
chat_id=admin_id, text=f"🤖 Я готов к работе, господин {name}!"
)
# сохраняем сообщение, отправленное админу
save_message(msg.chat.id, msg.message_id)
logger.info(f"Сообщение отправлено админу {admin_id} ({name})")
except Exception as e:
logger.error(f"Ошибка при отправке админу {admin_id}: {e}")
confirm_msg = await message.answer("✅ Всем админам отправлено приветствие.")
# сохраняем подтверждение пользователю
save_message(confirm_msg.chat.id, confirm_msg.message_id)