This commit is contained in:
@@ -10,7 +10,7 @@ API_TOKEN = os.getenv("BOT_TOKEN")
|
||||
|
||||
bot = Bot(token=API_TOKEN)
|
||||
dp = Dispatcher(bot)
|
||||
timer_manager = TimerManager(bot)
|
||||
timer_manager = None
|
||||
|
||||
menu_kb = InlineKeyboardMarkup(row_width=2)
|
||||
menu_kb.add(
|
||||
@@ -43,7 +43,9 @@ async def handle_callback(call: types.CallbackQuery):
|
||||
from pomodoro_bot.redis_client import init_redis
|
||||
|
||||
async def on_startup(dispatcher):
|
||||
global timer_manager
|
||||
await init_redis()
|
||||
timer_manager = TimerManager(bot, dispatcher.loop)
|
||||
|
||||
def run_bot():
|
||||
executor.start_polling(dp, skip_updates=True, on_startup=on_startup)
|
||||
@@ -3,8 +3,9 @@ from .user_manager import user_manager
|
||||
from .models import UserState
|
||||
|
||||
class TimerManager:
|
||||
def __init__(self, bot):
|
||||
def __init__(self, bot, loop):
|
||||
self.bot = bot
|
||||
self.loop = loop
|
||||
self.tasks = {}
|
||||
|
||||
async def start_timer(self, user_id, duration, chat_id, label):
|
||||
@@ -16,7 +17,7 @@ class TimerManager:
|
||||
await user_manager.increment_pomodoros(user_id)
|
||||
await self.bot.send_message(chat_id, f"⏰ {label} завершён!")
|
||||
|
||||
task = asyncio.create_task(timer())
|
||||
task = self.loop.create_task(timer())
|
||||
user = user_manager.get_user(user_id)
|
||||
user.current_timer = label
|
||||
user.task = task
|
||||
@@ -29,3 +30,15 @@ class TimerManager:
|
||||
user.task = None
|
||||
user.current_timer = None
|
||||
self.tasks.pop(user_id, None)
|
||||
|
||||
# pomodoro_bot/redis_client.py
|
||||
import redis.asyncio as redis
|
||||
import os
|
||||
|
||||
r = None
|
||||
|
||||
async def init_redis():
|
||||
global r
|
||||
r = redis.Redis.from_url(
|
||||
os.getenv("REDIS_URL", "redis://localhost"), decode_responses=True
|
||||
)
|
||||
Reference in New Issue
Block a user