/
Noriyki
/
VibeCodeBot
Обзор
Документация
Войти
/
Noriyki
/
VibeCodeBot
Код
Запросы
2
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/handlers/user.py
201 строка
7 KB
Chelovechec
feat(backend): создание репозитория для работы с командами
26 фев 2026, 18:33
26 фев 2026, 18:33
edde799
Код
Авторство
О чём код?
from telebot import types from src.DB.repositories import user_repo from src.DB.repositories import team_repo from src.DB.exeptions import TeamNotFoundException from src.services.everyday import ( set_daily_rating, get_daily_problem, mark_daily_done, ) from src.services.problem_picker import get_problem_by_rating from src.keyboards.main_menu import ( rating_inline_keyboard, rating_one_keyboard, tems_selection_keyboard, daily_done_and_reset_keyboard, daily_done_keyboard, one_done_keyboard, ) def _get_user_data(message: types.Message): user_id = message.from_user.id username = message.from_user.username or message.from_user.first_name or "user" chat_id = message.chat.id return user_id, username, chat_id def _remember_chat(message: types.Message): """Сохраняем chat_id для авторассылки.""" user_id, username, chat_id = _get_user_data(message) user_repo.add_or_update_user(user_id, username, chat_id=chat_id) def _safe_int(value: str): try: return int(value) except ValueError: return None def register_user_handlers(bot): """Обработчики действий пользователя.""" @bot.message_handler(func=lambda m: m.text == "🎯 Одна задача") def handle_one(message: types.Message): bot.send_message( message.chat.id, "Выберите рейтинг для задачи:", reply_markup=rating_one_keyboard(), ) @bot.callback_query_handler(func=lambda c: c.data.startswith("one_rating:")) def handle_one_rating_callback(call: types.CallbackQuery): bot.answer_callback_query(call.id) _, rating_str = call.data.split(":", 1) rating = _safe_int(rating_str) if rating is None: bot.send_message(call.message.chat.id, "Рейтинг должен быть числом") return bot.edit_message_reply_markup( chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=None, ) user_id = call.from_user.id username = call.from_user.username or call.from_user.first_name or "user" chat_id = call.message.chat.id text = get_problem_by_rating(rating, user_id, username) bot.send_message(chat_id, text, reply_markup=one_done_keyboard()) @bot.callback_query_handler(func=lambda c: c.data == "one_done") def handle_one_done_callback(call: types.CallbackQuery): user_id = call.from_user.id count = mark_daily_done(user_id) bot.answer_callback_query(call.id, text="Задача засчитана 👍") new_text = ( "🎯 Одна задача\n\n" "✅ Выполнено!\n" f"📊 В этом месяце: {count}" ) bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=new_text, reply_markup=None, ) @bot.message_handler(func=lambda m: m.text == "⚙️ Задать рейтинг") def handle_daily_rating(message: types.Message): bot.send_message( message.chat.id, "Выберите рейтинг ежедневной задачи:", reply_markup=rating_inline_keyboard(), ) @bot.callback_query_handler(func=lambda c: c.data.startswith("daily_rating:")) def handle_daily_rating_callback(call: types.CallbackQuery): _, rating_str = call.data.split(":") rating = int(rating_str) user_id = call.from_user.id username = call.from_user.username or call.from_user.first_name or "user" set_daily_rating(user_id, username, rating) bot.answer_callback_query(call.id, text=f"Ежедневный рейтинг установлен: {rating}") bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=f"✅ Ежедневный рейтинг: {rating}", ) @bot.message_handler(func=lambda m: m.text == "📌 Ежедневная задача") def handle_daily(message: types.Message): _remember_chat(message) user_id, username, chat_id = _get_user_data(message) text, is_new = get_daily_problem(user_id, username) if is_new: bot.send_message(chat_id, text, reply_markup=daily_done_and_reset_keyboard()) else: bot.send_message(chat_id, text) @bot.callback_query_handler(func=lambda c: c.data == "daily_done") def handle_daily_done_callback(call: types.CallbackQuery): user_id = call.from_user.id count = mark_daily_done(user_id) bot.answer_callback_query(call.id, text="Задача засчитана 👍") new_text = ( "📌 Ежедневная задача\n\n" "✅ Выполнено!\n" f"📊 В этом месяце: {count}" ) bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=new_text, reply_markup=None, ) @bot.callback_query_handler(func=lambda c: c.data == "resend_daily") def handle_resend_daily(call: types.CallbackQuery): user_id = call.from_user.id username = call.from_user.username or call.from_user.first_name or "user" chat_id = call.message.chat.id user_repo.reset_daily_problem(user_id) text, is_new = get_daily_problem(user_id, username) bot.answer_callback_query(call.id, text="Задача обновлена") bot.edit_message_text( chat_id=chat_id, message_id=call.message.message_id, text=text, reply_markup=daily_done_keyboard(), ) @bot.message_handler(func=lambda m: m.text == "🫂 Выбор команды") def handle_team_selection(message: types.Message): bot.send_message( message.chat.id, "Выберите команду:", reply_markup=tems_selection_keyboard(), ) @bot.callback_query_handler(func=lambda c: c.data.startswith("team_select:")) def handle_team_selection_callback(call: types.CallbackQuery): user_id = call.from_user.id username = call.from_user.username or call.from_user.first_name or "user" _, team_name = call.data.split(":") try: team_id = team_repo.get_team_id_by_name(team_name) except TeamNotFoundException: bot.answer_callback_query(call.id, "❌ Команда не найдена") return user_repo.add_or_update_user(user_id, username) user_repo.set_user_team(user_id, team_id) bot.answer_callback_query(call.id, text=f"Вы выбрали команду: {team_name}") bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text=f"✅ Вы выбрали команду: {team_name}", reply_markup=None, )