DispelEvilBot/bot/handlers/economy.py

71 lines
2.4 KiB
Python

"""Команды экономики: очищение, яички и реакция на сообщения."""
from __future__ import annotations
import random
from datetime import date, datetime
from aiogram import Router
from aiogram.filters import Command
from aiogram.types import Message
from bot.services import access, economy as economy_service
from common.config import get_settings
from common.texts import economy, system
from common.texts.render import render
router = Router(name="economy")
#: Окно сезонного события «яички».
EGG_EVENT_START = date(2025, 4, 1)
EGG_EVENT_END = date(2025, 4, 20)
#: Шанс выпадения монетки на сообщение.
COIN_CHANCE = 27 # из 100
@router.message(Command("очищение", ignore_case=True))
async def purification_handler(message: Message) -> None:
if not await access.check_user(message):
return
result = await economy_service.purify(message.from_user.id)
if not result.success:
context = {"h": result.hours, "m": result.minutes, "s": result.seconds}
await message.answer(render(economy.MSG_NO_PURIFICATION, context))
else:
context = {"money_add": result.money_add, "exp_add": result.exp_add}
await message.answer(render(economy.MSG_PURIFICATION, context))
@router.message(Command("яички", ignore_case=True))
async def eggs_handler(message: Message) -> None:
today = datetime.now(get_settings().tzinfo).date()
if not (EGG_EVENT_START <= today <= EGG_EVENT_END):
await message.answer(system.MSG_EGGS_TOO_EARLY)
return
if not await access.check_user(message):
return
result = await economy_service.collect_eggs(message.from_user.id)
if not result.success:
context = {"h": result.hours, "m": result.minutes, "s": result.seconds}
await message.answer(render(economy.MSG_NO_EGGS, context))
else:
await message.answer(render(economy.MSG_EGGS, {"eggs": result.eggs}))
@router.message()
async def message_handler(message: Message) -> None:
"""Случайная монетка за активность в основном чате."""
if not await access.check_user(message):
return
if message.chat.id != get_settings().chat_id:
return
if random.randrange(1, 100) == COIN_CHANCE:
await message.reply(system.MSG_COIN)
await economy_service.add_coins(message.from_user.id, 1)