|
2 | 2 | from telebot.handler_backends import StatesGroup, State |
3 | 3 | from datetime import datetime |
4 | 4 |
|
5 | | -from db_adapter import * |
6 | | - |
7 | | -from init_bot import bot |
8 | | - |
| 5 | +from db_adapter import ( |
| 6 | + add_question_to_db, |
| 7 | + add_choice_to_db, |
| 8 | +) |
9 | 9 |
|
10 | 10 |
|
11 | 11 | class AdditionQuestion(StatesGroup): |
12 | 12 | waiting_question = State() |
13 | 13 | waiting_choices = State() |
14 | 14 |
|
15 | | -ADMIN_IDS = [68360888707] |
16 | | - |
17 | | -def is_admin(func): |
18 | | - def wrapper(*args, **kwargs): |
19 | | - message = args[0] |
20 | | - if message.from_user.id in ADMIN_IDS: |
21 | | - func(*args, **kwargs) |
22 | | - else: |
23 | | - bot.reply_to(message, "Нет прав") |
24 | | - return wrapper |
25 | | - |
26 | | -@is_admin |
27 | | -@bot.message_handler(commands=['create_question']) |
28 | | -def start_addition_question(message: telebot.types.Message): |
29 | | - if message.from_user.id in ADMIN_IDS: |
| 15 | + |
| 16 | +ADMIN_IDS = [ |
| 17 | + 369937974, # @the_real_math |
| 18 | +] |
| 19 | + |
| 20 | +logger = telebot.logger |
| 21 | + |
| 22 | + |
| 23 | +def register_handlers(bot): |
| 24 | + def is_admin(func): |
| 25 | + def wrapper(*args, **kwargs): |
| 26 | + message = args[0] |
| 27 | + if message.from_user.id in ADMIN_IDS: |
| 28 | + func(*args, **kwargs) |
| 29 | + else: |
| 30 | + bot.reply_to(message, "Нет прав") |
| 31 | + return wrapper |
| 32 | + |
| 33 | + @bot.message_handler(commands=['create_question']) |
| 34 | + @is_admin |
| 35 | + def start_addition_question(message: telebot.types.Message): |
30 | 36 | bot.reply_to(message, "Введите вопрос") |
31 | 37 | bot.set_state(message.from_user.id, AdditionQuestion.waiting_question) |
32 | | - else: |
33 | | - bot.reply_to(message, "Нет прав") |
34 | | - |
35 | | - |
36 | | -@is_admin |
37 | | -@bot.message_handler(state=AdditionQuestion.waiting_question) |
38 | | -def wait_question(message: telebot.types.Message): |
39 | | - bot.reply_to(message, "Вопрос получен. Введите варианты ответа") |
40 | | - question_text = message.text |
41 | | - publish_date = datetime.now() |
42 | | - inserted_question_id = add_question_to_db(question_text, publish_date) |
43 | | - with bot.retrieve_data(message.from_user.id) as data: |
44 | | - data['question_id'] = inserted_question_id |
45 | | - bot.set_state(message.from_user.id, AdditionQuestion.waiting_choices) |
46 | | - |
47 | | - |
48 | | -@is_admin |
49 | | -@bot.message_handler(state=AdditionQuestion.waiting_choices) |
50 | | -def wait_choice(message: telebot.types.Message): |
51 | | - bot.reply_to(message, "Ответы получены. Сохранил") |
52 | | - with bot.retrieve_data(message.from_user.id) as data: |
53 | | - inserted_question_id = data['question_id'] |
54 | | - for x in message.text.split('\n'): |
55 | | - add_choice_to_db(x, inserted_question_id) |
| 38 | + |
| 39 | + @bot.message_handler(state=AdditionQuestion.waiting_question) |
| 40 | + @is_admin |
| 41 | + def wait_question(message: telebot.types.Message): |
| 42 | + bot.reply_to(message, "Вопрос получен. Введите варианты ответа") |
| 43 | + question_text = message.text |
| 44 | + publish_date = datetime.now() |
| 45 | + inserted_question_id = add_question_to_db(question_text, publish_date) |
| 46 | + with bot.retrieve_data(message.from_user.id) as data: |
| 47 | + data['question_id'] = inserted_question_id |
| 48 | + bot.set_state(message.from_user.id, AdditionQuestion.waiting_choices) |
| 49 | + |
| 50 | + @bot.message_handler(state=AdditionQuestion.waiting_choices) |
| 51 | + @is_admin |
| 52 | + def wait_choice(message: telebot.types.Message): |
| 53 | + bot.reply_to(message, "Ответы получены. Сохранил") |
| 54 | + with bot.retrieve_data(message.from_user.id) as data: |
| 55 | + inserted_question_id = data['question_id'] |
| 56 | + for x in message.text.split('\n'): |
| 57 | + add_choice_to_db(x, inserted_question_id) |
0 commit comments