コード例 #1
0
def _review_answer_handler(user: User, update: Update,
                           context: CallbackContext):
    mute_user(user.id)
    available_inputs = [
        user.language.get(Token.AGREE_NICE_EXAMPLE),
        user.language.get(Token.DO_NOT_LIKE_EXAMPLE),
        user.language.get(Token.REPORT_SUBMISSION),
        user.language.get(Token.QUIT_REVIEWING)
    ]

    if update.message.text not in available_inputs:
        reply_to(user, update,
                 user.language.get(Token.PLEASE_ENTER_VALID_REVIEW),
                 Keyboard.review_keyboard(user.language))
        return

    submission = context.user_data["submission"]
    context.user_data["review_count"] += 1

    if update.message.text == user.language.get(Token.AGREE_NICE_EXAMPLE):
        add_review(user, submission, ReviewCategory.LIKE)
        reply_to(
            user, update,
            user.language.get(Token.THANKS_FOR_REVIEW) %
            (get_random_congrats_message(
                user.language), submission_scores.get_review_score()))
        _process_review_achievements(user, update)
        notification_manager.send_someone_liked_your_example(
            submission.user, context)
        scoreboard.iterate(update, context)
    elif update.message.text == user.language.get(Token.DO_NOT_LIKE_EXAMPLE):
        add_review(user, submission, ReviewCategory.DISLIKE)
        reply_to(
            user, update,
            user.language.get(Token.THANKS_FOR_REVIEW) %
            (get_random_congrats_message(
                user.language), submission_scores.get_review_score()))
        _process_review_achievements(user, update)
        scoreboard.iterate(update, context)
    elif update.message.text == user.language.get(Token.REPORT_SUBMISSION):
        add_review(user, submission, ReviewCategory.SKIP)
        reply_to(user, update,
                 user.language.get(Token.REPORT_SUBMISSION_REPLY))
        context.bot.send_message(
            mwexpress_config.moderator,
            f"Someone reported this submission: {submission.value}\n"
            f"Flag submission: /flag{submission.id}, "
            f"ban user: /ban{submission.user.id}")
        _process_review_achievements(user, update)
        scoreboard.iterate(update, context)
    else:
        unmute_user(user.id)
        reply_to(user, update, user.language.get(Token.REVIEW_CANCELLED),
                 Keyboard.main(user))
        _safe_delete_context_data(context, "submission")
        clear_state(context)
        return

    _send_submission_to_review(user, update, context)
コード例 #2
0
def _send_submission_to_review(user: User, update: Update,
                               context: CallbackContext):
    if not user.viewed_review_help:
        reply_to(user, update, user.language.get(Token.REVIEW_HELP_MESSAGE_1))
        time.sleep(4)
        reply_to(user, update, user.language.get(Token.REVIEW_HELP_MESSAGE_2))
        time.sleep(4)
        user.viewed_review_help = True
        update_user(user)

    todays_mwe = get_todays_mwe(user.language)
    submissions = get_submissions_to_review(todays_mwe, user)

    if len(submissions) > 0:
        submission: Submission = submissions[0]
        context.user_data["submission"] = submission
        if "review_count" not in context.user_data:
            context.user_data["review_count"] = 0

        parsed = parser.parse(submission.value, submission.mwe)
        review_example = submission.value
        for index in reversed(sorted(submission.mwe_indices)):
            start_index = parsed.token_positions[index][0]
            end_index = parsed.token_positions[index][1]
            review_example = review_example[:
                                            end_index] + "</u></b>" + review_example[
                                                end_index:]
            review_example = review_example[:
                                            start_index] + "<b><u>" + review_example[
                                                start_index:]

        if submission.category == SubmissionCategory.POSITIVE_SEPARATED or \
                submission.category == SubmissionCategory.POSITIVE_TOGETHER:
            review_question = user.language.get(Token.REVIEW_QUESTION_POSITIVE) \
                              % (review_example, _get_word_list_str_from_submission(submission))
            reply_html(user, update, review_question,
                       Keyboard.review_keyboard(user.language))
        else:
            review_question = user.language.get(Token.REVIEW_QUESTION_NEGATIVE) \
                              % (review_example, _get_word_list_str_from_submission(submission))
            reply_html(user, update, review_question,
                       Keyboard.review_keyboard(user.language))
    else:
        if "review_count" in context.user_data:
            reply_to(user, update,
                     user.language.get(Token.NO_SUB_LEFT_TO_REVIEW),
                     Keyboard.main(user))
        else:
            reply_to(user, update, user.language.get(Token.NO_SUBMISSIONS),
                     Keyboard.main(user))
        clear_state(context)
        unmute_user(user.id)
        _safe_delete_context_data(context, "submission")
        _safe_delete_context_data(context, "review_count")
コード例 #3
0
def main_review_handler(user: User, update: Update, context: CallbackContext):
    now = datetime.now().time()
    if mwexpress_config.start_time <= now < mwexpress_config.end_time:
        set_state(context, State.REVIEWING)

        if "submission" in context.user_data:
            _review_answer_handler(user, update, context)
            return

        _send_submission_to_review(user, update, context)
    else:
        clear_state(context)
        unmute_user(user.id)
        _safe_delete_context_data(context, "submission")
        _safe_delete_context_data(context, "review_count")
        reply_to(user,
                 update,
                 user.language.get(Token.GAME_HOURS_FINISHED) %
                 mwexpress_config.start_time.hour,
                 reply_markup=Keyboard.main(user))
コード例 #4
0
def start(update: Update, context: CallbackContext):
    user = get_user_from_update(update)

    mwelog.info("User {user_name} started using Mwexpress",
                user_name=user.username,
                user_id=user.id)

    clear_state(context)

    context.bot.send_sticker(update.effective_chat.id, TIPS_FEDORA_STICKER)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_1),
             Keyboard.remove())
    time.sleep(2)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_2),
             Keyboard.remove())
    time.sleep(5)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_3),
             Keyboard.remove())
    time.sleep(3)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_4),
             Keyboard.remove())
    time.sleep(2)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_5),
             Keyboard.remove())
    time.sleep(5)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_6),
             Keyboard.remove())
    time.sleep(10)
    update.message.reply_text(text=user.language.get(Token.WELCOME_MESSAGE_7),
                              parse_mode=ParseMode.HTML,
                              reply_markup=Keyboard.remove())
    time.sleep(5)
    context.bot.send_photo(user.id, open("assets/keyboard_button.png", "rb"))
    time.sleep(0.5)
    reply_to(user, update, user.language.get(Token.WELCOME_MESSAGE_8))
    time.sleep(5)
    reply_to(user, update, user.language.get(Token.DISCLAIMER),
             Keyboard.main(user))
コード例 #5
0
def language_update_handler(user: User, update: Update,
                            context: CallbackContext) -> None:
    if update.message.text == user.language.get(Token.LANGUAGE_ENGLISH):
        change_user_language(user, Language.ENGLISH)
        clear_state(context)
        reply_to(user, update,
                 user.language.get(Token.LANGUAGE_CHANGE_SUCCESSFUL),
                 Keyboard.main(user))
    elif update.message.text == user.language.get(Token.LANGUAGE_TURKISH):
        change_user_language(user, Language.TURKISH)
        clear_state(context)
        reply_to(user, update,
                 user.language.get(Token.LANGUAGE_CHANGE_SUCCESSFUL),
                 Keyboard.main(user))
    else:
        mwelog.info(
            "User {user_name} entered wrong value ({message}) for language change.",
            user_name=user.username,
            user_id=user.id,
            message=update.message.text)
        reply_to(user, update,
                 user.language.get(Token.PLEASE_SELECT_VALID_LANGUAGE),
                 Keyboard.language_selection(user.language))
コード例 #6
0
def _clear_context(context: CallbackContext):
    clear_state(context)
    _safe_delete_context_data(context, "sub_state")
    _safe_delete_context_data(context, "parsed")
コード例 #7
0
ファイル: email.py プロジェクト: Dodiom/dodiom
def _clear_context(context: CallbackContext):
    clear_state(context)
    if "sub_state" in context.user_data:
        del context.user_data["sub_state"]
    if "email" in context.user_data:
        del context.user_data["email"]
コード例 #8
0
ファイル: message.py プロジェクト: Dodiom/dodiom
def message(update: Update, context: CallbackContext):
    user = get_user_from_update(update)
    try:
        mwelog.info("New message from {user_name}: {message}",
                    user_name=user.username,
                    message=update.message.text)

        if user.banned:
            update.message.reply_text(
                user.language.get(Token.USER_IS_BANNED_MESSAGE))
            return

        if update.message.text.startswith("/flag"):
            flag_submission(user, int(update.message.text.replace("/flag",
                                                                  "")),
                            context)
            return
        if update.message.text.startswith("/ban"):
            ban_user(user, int(update.message.text.replace("/ban", "")),
                     context)
            return

        if mwexpress_config.game_stopped:
            update.message.reply_sticker(COFFEE_STICKER)
            update.message.reply_text(
                user.language.get(Token.GAME_TEMPORARILY_STOPPED))
            update.message.reply_text(user.language.get(Token.SURVEY_MESSAGE))
            return

        if get_state(context) != State.NONE:
            state = get_state(context)
            mwelog.info("Current state for {user_name}: {state}",
                        user_name=user.username,
                        state=str(state))
            if state == State.SUBMISSION:
                main_submit_handler(user, update, context)
            elif state == State.CHANGING_LANGUAGE:
                language_update_handler(user, update, context)
            elif state == State.REVIEWING:
                main_review_handler(user, update, context)
            elif state == State.ADDING_EMAIL:
                main_email_handler(user, update, context)
        else:
            if update.message.text == user.language.get(Token.TODAYS_MWE):
                todays_mwe_handler(user, update)
            elif update.message.text == user.language.get(Token.SUBMIT):
                main_submit_handler(user, update, context)
            elif update.message.text == user.language.get(
                    Token.CHANGE_LANGUAGE):
                language_change_handler(user, update, context)
            elif update.message.text == user.language.get(Token.HELP):
                help_handler(user, update, context)
            elif update.message.text == user.language.get(Token.REVIEW):
                main_review_handler(user, update, context)
            elif update.message.text == user.language.get(Token.FEEDBACK):
                feedback_handler(user, update, context)
            elif update.message.text == user.language.get(
                    Token.SHOW_SCOREBOARD):
                scoreboard_handler(user, update, context)
            elif update.message.text == user.language.get(Token.ACHIEVEMENTS):
                achievements_handler(user, update, context)
            elif update.message.text == user.language.get(Token.ADD_EMAIL):
                main_email_handler(user, update, context)
            elif submission_contains_todays_mwe(user, update.message.text):
                context.user_data["sub_state"] = "typing_example"
                main_submit_handler(user, update, context)
            else:
                update.message.reply_text(user.language.get(
                    Token.ENTER_VALID_COMMAND),
                                          parse_mode=ParseMode.MARKDOWN,
                                          reply_markup=Keyboard.main(user))
                context.bot.send_photo(
                    user.id, open("assets/keyboard_button.png", "rb"))
                update.message.reply_text(user.language.get(
                    Token.WELCOME_MESSAGE_8),
                                          parse_mode=ParseMode.MARKDOWN,
                                          reply_markup=Keyboard.main(user))

    except Exception as ex:
        clear_state(context)
        unmute_user(user.id)
        _safe_delete_context_data(context, "sub_state")
        _safe_delete_context_data(context, "parsed")
        _safe_delete_context_data(context, "submission")
        mwelog.error(
            f"erroneous message: {user.username}: {update.message.text}")
        mwelog.exception(str(ex))
        update.message.reply_text(user.language.get(Token.ERROR_OCCURRED),
                                  reply_markup=Keyboard.main(user))