Example #1
0
 def get(
     cls, poll_id: int
 ) -> "Poll":  # Evaluate the return type after the class is finished processing
     with get_connection() as connection:
         poll = database.get_poll(connection, poll_id)
         return cls(poll[1], poll[2], poll[0])
Example #2
0
def handle_callback_vote(bot, call):
    # Получение голосовалки
    poll = database.get_poll(
        chat_id=call.message.chat.id,
        message_id=call.message.message_id,
    )

    if not poll:
        bot.answer_callback_query(
            callback_query_id=call.id,
            text=callback_error_no_poll,
            show_alert=True,
        )
        return False

    # Получение голоса
    vote = database.get_vote(
        poll_id=poll.id,
        voted_id=call.from_user.id,
    )

    # Создание/обновление/игнорирование голоса
    if not vote:
        database.create_vote(
            poll_id=poll.id,
            voted_id=call.from_user.id,
            to_ban=(call.data == "vote_for"),
        )
    elif ((vote.to_ban and call.data == "vote_for")
          or (not vote.to_ban and call.data == "vote_against")):
        bot.answer_callback_query(
            callback_query_id=call.id,
            text=callback_error_vote_counted,
            show_alert=False,
        )
        return True
    else:
        database.update_vote(
            id=vote.id,
            to_ban=(call.data == "vote_for"),
        )

    # Получение результатов голосования
    poll_results = database.get_poll_results(poll_id=poll.id, )

    # Подстановка результатов в клавиатуру
    bot.edit_message_reply_markup(
        chat_id=call.message.chat.id,
        message_id=call.message.message_id,
        reply_markup=create_poll_keyboard(
            votes_for_amount=poll_results["votes_for_amount"],
            votes_against_amount=poll_results["votes_against_amount"],
        ),
    )

    # Нотификейшн о выбранном варианте ответа
    bot.answer_callback_query(
        callback_query_id=call.id,
        text=callback_message.format("Да" if call.data ==
                                     "vote_for" else "Нет"),
        show_alert=False,
    )

    # Получение настроек бота
    settings = database.get_settings(chat_id=call.message.chat.id)

    # Если голосов не достаточно для принятия решения - выход
    if ((poll_results["votes_for_amount"] < settings.votes_for_decision) and
        (poll_results["votes_against_amount"] < settings.votes_for_decision)):
        return True

    # Получение инфы об обвиняемом
    accused = bot.get_chat_member(
        chat_id=call.message.chat.id,
        user_id=poll.accused_id,
    )
    accused_full_name = "{} {}".format(
        accused.user.first_name,
        accused.user.last_name,
    )

    # Большинство проголосовало против - выход
    if (poll_results["votes_against_amount"] >= settings.votes_for_decision):
        bot.send_message(
            chat_id=call.message.chat.id,
            text=result_message_innocent.format(
                accused_full_name,
                poll.accused_id,
                poll.accused_message,
            ),
            parse_mode="markdown",
        )
        bot.delete_message(
            chat_id=call.message.chat.id,
            message_id=poll.message_id,
        )
        database.delete_poll(poll.id)
        return True

    # Определяем тип наказания
    punishment = "ban" if poll.reason == "spam" else settings.punishment

    # Применение наказания
    if (punishment == "ban"):
        bot.kick_chat_member(
            chat_id=call.message.chat.id,
            user_id=poll.accused_id,
            until_date=time.time(),
        )
    elif (punishment == "kick"):
        bot.kick_chat_member(
            chat_id=call.message.chat.id,
            user_id=poll.accused_id,
            until_date=time.time() + 60,
        )
    else:
        bot.restrict_chat_member(
            chat_id=call.message.chat.id,
            user_id=poll.accused_id,
            until_date=time.time() + settings.days * 86400,
        )

    # Сообщение о наказании
    bot.send_message(
        chat_id=call.message.chat.id,
        text=result_message_guilty.format(
            accused_full_name,
            poll.accused_id,
            "Спам" if poll.reason == "spam" else poll.accused_message,
            punishment,
        ),
        parse_mode="markdown",
    )

    # Удаление сообщения с голосовалкой
    bot.delete_message(
        chat_id=call.message.chat.id,
        message_id=poll.message_id,
    )

    # Удаление сообщения со спамом
    if poll.reason == "spam":
        bot.delete_message(
            chat_id=call.message.chat.id,
            message_id=poll.accused_message_id,
        )

    # Удаление голосовалки
    database.delete_poll(poll.id)

    return True
Example #3
0
 def get(cls, poll_id: int) -> "Poll":
     with get_connection() as connection:
         poll = database.get_poll(connection, poll_id)
         return cls(poll[1], poll[2], poll[0])
Example #4
0
 def get(cls, poll_id: int) -> "Poll":
     connection = create_connection()
     poll = database.get_poll(connection, poll_id)
     connection.close()
     return cls(poll[1], poll[2], poll[0])
 def get(cls, poll_id: int) -> "Poll":
     connection = pool.getconn()
     poll = database.get_poll(connection, poll_id)
     pool.putconn(connection)
     return cls(poll[1], poll[2], poll[0])