示例#1
0
 def check_answer(self, bot: Bot, update: Update) -> None:
     user_id: int = self.query.from_user.id
     name: str = f"user:questions:{user_id}"
     name_results: str = f"user:results:{user_id}"
     answer_data = parse_list(self.query.data, splitter=":")
     print(answer_data)
     chosen = rdb.hget(
         name=name_results,
         key=answer_data[1]).decode("utf-8")
     if chosen != "u":
         bot.answerCallbackQuery(
             callback_query_id=self.query.id,
             text=config["STRINGS"]["already_chosen"],
             show_alert=True)
         return
     else:
         user_questions = json.loads(rdb.get(name))
         current_question = user_questions[answer_data[1]]
         choice_string = "c" if int(current_question["answer"]) \
             == answer_data[2] else "w"
         print(choice_string)
         print(current_question)
         rdb.hset(
             name=name_results,
             key=answer_data[1],
             value=choice_string)
         self.make_keyboard(bot, update)
示例#2
0
 def diverter(self, bot: Bot, update: Update) -> None:
     self.query = update.callback_query
     print(self.query.data)
     if (self.query.data == "ready" or self.query.data[0] == "ready"):
         self.ready_handler(bot, update)
     elif "forward" in self.query.data or "back" in self.query.data:
         self.forward_or_back_handler(bot, update)
     elif "answer" in self.query.data:
         self.check_answer(bot, update)
     elif "ok_cool" in self.query.data:
         pass
     else:
         pass
     bot.answerCallbackQuery(
             callback_query_id=self.query.id)
示例#3
0
    def forward_or_back_handler(self, bot: Bot, update: Update) -> None:
        # TODO: this is very unstable and buggy, fix.
        user_id: int = self.query.from_user.id
        name: str = f"user:{user_id}"
        question_number = int(rdb.hget(name, "question"))
        max_number = int(config["GENERAL"]["questions_count"]) - 1
        min_correct = int(config["GENERAL"]["correct_answers"])
        if "forward" in self.query.data:
            rdb.hincrby(name, "question", 1)
        elif "back" in self.query.data:
            rdb.hincrby(name, "question", -1)

        if question_number > max_number:
            rdb.hset(name, "question", -1)
        elif question_number == max_number:
            user_results = get_results(decode_dict(
                rdb.hgetall(f"user:results:{user_id}")))
            if user_results["u"] == 0:
                if user_results["c"] >= min_correct:
                    bot.answerCallbackQuery(
                        callback_query_id=self.query.id,
                        text=config["STRINGS"]["enough_correct"],
                        show_alert=True)
                    for chat_id in parse_list(config["CHATS"]["main_chats"]):
                            bot.restrictChatMember(
                                chat_id=chat_id,
                                user_id=user_id,
                                can_send_messages=True,
                                can_send_media_messages=True,
                                can_send_other_messages=True,
                                can_add_web_page_previews=True)
                else:
                    rdb.setnx(f"user:wait:{user_id}", time.time())
                    bot.answerCallbackQuery(
                        callback_query_id=self.query.id,
                        text=config["STRINGS"]["has_to_wait"],
                        show_alert=True)
            else:
                bot.answerCallbackQuery(
                        callback_query_id=self.query.id,
                        text=config["STRINGS"]["unanswered_questions"],
                        show_alert=True)
            return
        elif question_number < 0:
            rdb.hincrby(name, "question", 1)

        self.make_keyboard(bot, update)
示例#4
0
    def make_keyboard(self, bot: Bot, update: Update) -> None:
        user_id: int = self.query.from_user.id
        name: str = f"user:questions:{user_id}"
        name_results: str = f"user:results:{user_id}"
        user_questions = json.loads(rdb.get(name))

        question_number = int(rdb.hget(f"user:{user_id}", "question"))
        questions_count = int(config["GENERAL"]["questions_count"])
        if question_number < 0 or question_number >= questions_count:
            question_number = 0
            rdb.hset(f"user:{user_id}", "question", question_number)

        current_question = user_questions[question_number]
        question_id = quizzes["quizzes"].index(current_question)
        user_question_id = user_questions.index(current_question)
        keyboard = [[], []]
        rdb.hsetnx(
            name=name_results,
            key=user_question_id,
            value="u")
        chosen = rdb.hget(
            name=name_results,
            key=user_question_id).decode("utf-8")

        text = f"<code>(ID: {question_id})</code>\n" \
               f"{current_question['question']}\n"

        keyboard[1].append(InlineKeyboardButton(
            "<", callback_data=f"back"))
        keyboard[1].append(InlineKeyboardButton(
            f"{question_number + 1}/{config['GENERAL']['questions_count']}",
            callback_data=f"ok_cool"))
        keyboard[1].append(InlineKeyboardButton(
            ">", callback_data=f"forward"))

        # TODO: make this pretty.
        if chosen == "u":
            for option in current_question["options"]:
                option_index = current_question["options"].index(option)
                keyboard[0].append(InlineKeyboardButton(
                    str(option_index),
                    callback_data=f"answer:{user_question_id}:{option_index}",
                    parse_mode="HTML"))
                text += f"\n{option_index}) {option}"
        elif chosen == "c" or chosen == "w":
            for option in current_question["options"]:
                option_index = current_question["options"].index(option)
                text += f"\n{option_index}) {option}"
            choice = config["STRINGS"]["correct_choice"] if chosen == "c" \
                else config["STRINGS"]["wrong_choice"]
            text += f"\n\n<i>{choice}</i>"

        kwargs = {
            "chat_id": self.query.message.chat.id,
            "text": text,
            "reply_markup": InlineKeyboardMarkup(keyboard),
            "parse_mode": "HTML"}

        # had this as one line, but flake8... :(
        sendQuiz = bot.editMessageText
        kwargs["message_id"] = self.query.message.message_id
        sendQuiz(**kwargs)
        bot.answerCallbackQuery(
            callback_query_id=self.query.id)