def confirm_email(user: User, update: Update, context: CallbackContext): available_inputs = [ user.language.get(Token.YES), user.language.get(Token.NO), user.language.get(Token.CANCEL) ] message = update.message.text if message not in available_inputs: reply_to(user, update, user.language.get(Token.ENTER_VALID_COMMAND), reply_markup=Keyboard.email_verify_keyboard(user.language)) return if message == user.language.get(Token.YES): email = context.user_data["email"] user.email = email update_user(user) _clear_context(context) reply_html(user, update, user.language.get(Token.EMAIL_SET) % email, Keyboard.main(user)) mwelog.info(f"{user.username} confirms {email} is correct") try: context.bot.send_message( mwexpress_config.moderator, f"{user.username} set their email as: {email}") except Exception as ex: mwelog.exception(ex) else: _clear_context(context) reply_to(user, update, user.language.get(Token.EMAIL_CANCELLED), Keyboard.main(user))
def stats(update: Update, context: CallbackContext): message = update.message.text.replace("/announce ", "") if len(message) > 10: for user in get_all_users(): try: send_message_to_user(context.bot, user, message) time.sleep(0.3) except Exception as ex: mwelog.exception(ex)
def submission_contains_todays_mwe(user: User, submission: str) -> bool: todays_mwe = get_todays_mwe(user.language) if parser.get_sentence_count(submission) != 1: return False try: parsed = parser.parse(submission, todays_mwe) return parsed.contains_mwe(todays_mwe) except Exception as ex: mwelog.exception(str(ex)) return False
def send_message_to_user(bot: Bot, user: User, msg: str, reply_markup: ReplyMarkup = None, parse_mode: str = ParseMode.MARKDOWN) -> None: try: bot.send_message(chat_id=user.id, text=msg, parse_mode=parse_mode, reply_markup=reply_markup) except Exception as ex: mwelog.exception(str(ex))
def claim_email_announcement(update: Update, context: CallbackContext): mwelog.info("Sending claim email announcement to champions") user = get_user_from_update(update) if user.id == mwexpress_config.moderator or user.id == 1065263859: for user in get_all_users(): if user.became_champion: try: mwelog.info( f"Sending claim email message to {user.username}") context.bot.send_sticker(user.id, EXCITED_STICKER) context.bot.send_message( user.id, user.language.get(Token.CHAMP_BUT_NO_EMAIL)) time.sleep(0.5) except Exception as ex: mwelog.exception(str(ex))
def send_game_started_again_with_awards(update: Update, context: CallbackContext): mwelog.info("Sending game started again message to all users") user = get_user_from_update(update) if user.id == mwexpress_config.moderator or user.id == 1065263859: for user in get_all_users(): try: mwelog.info( f"Sending game started again message to {user.username}") context.bot.send_sticker(user.id, EXCITED_STICKER) send_message_to_user( context.bot, user, user.language.get(Token.GAME_STARTED_AGAIN_ANNOUNCEMENT)) time.sleep(0.5) except Exception as ex: mwelog.exception(str(ex))
def send_game_starting_message_to_all() -> None: mwelog.info("Sending game started message to all users") unmute_everyone() all_users = get_all_users() for user in all_users: try: mwexpress_bot.bot.send_sticker(user.id, GOOD_MORNING_STICKER) send_message_to_user(mwexpress_bot.bot, user, user.language.get(Token.GAME_STARTED)) todays_mwe = get_todays_mwe(user.language) send_message_to_user( mwexpress_bot.bot, user, user.language.get(Token.TODAYS_MWE_REPLY_TEXT) % (todays_mwe.name, todays_mwe.meaning), reply_markup=Keyboard.main(user), parse_mode=ParseMode.HTML) time.sleep(0.3) except Exception as ex: mwelog.exception(str(ex)) mwelog.info("Sent game started message to all users")
def _send_notification(self, context: CallbackContext, user_id: int, message: str, not_type: NotificationType, sticker: Optional[str] = None, parse_mode: Optional[str] = None): if not self._history_contains_notification(user_id, not_type): try: mwelog.info("Sending {not_type} to {user_id}", not_type=str(not_type), user_id=user_id) notification = SentNotification(user_id, not_type, datetime.now()) self._notification_history[user_id].append(notification) if sticker is not None: context.bot.send_sticker(user_id, sticker) context.bot.send_message(user_id, message, parse_mode=parse_mode) except Exception as ex: mwelog.exception(str(ex))
def award_champion(): mwelog.info("Awarding champion") boards = scoreboard.scoreboards for language in Language.ENGLISH, Language.TURKISH, Language.ITALIAN: if len(boards[language]) > 0: first_user = get_user(boards[language][0].user_id) mwelog.info("{username} is the champion", username=first_user.username) award_achievement(first_user, AchievementType.CHAMPION) try: mwexpress_bot.bot.send_sticker(first_user.id, ACHIEVEMENT_STICKER) send_message_to_user(mwexpress_bot.bot, first_user, first_user.language.get( Token.CHAMPION_ACH_CONGRATS_MSG), parse_mode=ParseMode.HTML) if mwexpress_config.email_enabled: if first_user.email is None: send_message_to_user( mwexpress_bot.bot, first_user, first_user.language.get( Token.TODAYS_WINNER_WITHOUT_EMAIL), parse_mode=ParseMode.HTML) else: send_message_to_user( mwexpress_bot.bot, first_user, first_user.language.get( Token.TODAYS_WINNER_WITH_EMAIL) % first_user.email, parse_mode=ParseMode.HTML) mwexpress_bot.bot.send_message( mwexpress_config.moderator, f"Todays champion is: {first_user.username}") except Exception as ex: mwelog.exception(ex)
def iterate(self, update: Update, context: CallbackContext): with self._iterate_lock: new_boards = { Language.TURKISH: self.get_scoreboard(Language.TURKISH), Language.ENGLISH: self.get_scoreboard(Language.ENGLISH), Language.ITALIAN: self.get_scoreboard(Language.ITALIAN) } for language in Language.ENGLISH, Language.TURKISH, Language.ITALIAN: if len(new_boards[language]) > 0: first_user = get_user(new_boards[language][0].user_id) if not user_has_achievement( first_user, AchievementType.BECOME_NUMBER_ONE): award_achievement(first_user, AchievementType.BECOME_NUMBER_ONE) try: context.bot.send_sticker(first_user.id, ACHIEVEMENT_STICKER) context.bot.send_message( first_user.id, first_user.language.get( Token.BECOME_NUMBER_ONE_ACH_CONGRATS_MSG), parse_mode=ParseMode.HTML) except Exception as ex: mwelog.exception(str(ex)) else: if len(self.scoreboards[language]) == 0\ or (len(self.scoreboards[language]) > 1 and (self.scoreboards[language][1].user_id == first_user.id)): notification_manager.send_became_first( first_user, context) if len(new_boards[language]) > 1 and len( self.scoreboards[language]) > 1: if new_boards[language][1].user_id == self.scoreboards[ language][0].user_id: second = get_user(new_boards[language][1].user_id) notification_manager.send_lost_first(second, context) if len(new_boards[language]) > 3 and len( self.scoreboards[language]) > 2: if new_boards[language][3].user_id == self.scoreboards[ language][2].user_id: fourth_user = get_user(new_boards[language][3].user_id) notification_manager.send_lost_three( fourth_user, context) if len(new_boards[language]) > 5 and len( self.scoreboards[language]) > 4: if new_boards[language][5].user_id == self.scoreboards[ language][4].user_id: sixth_user = get_user(new_boards[language][5].user_id) notification_manager.send_lost_five( sixth_user, context) self.old_scoreboards = self.scoreboards self.scoreboards = new_boards first_five_now = { Language.TURKISH: [x.user_id for x in self.scoreboards[Language.TURKISH][:5]], Language.ENGLISH: [x.user_id for x in self.scoreboards[Language.ENGLISH][:5]], Language.ITALIAN: [x.user_id for x in self.scoreboards[Language.ITALIAN][:5]] } for language in Language.TURKISH, Language.ENGLISH, Language.ITALIAN: if self._new_first_five_ids[language] != first_five_now[ language]: self._old_first_five_ids[ language] = self._new_first_five_ids[language] self._new_first_five_ids[language] = first_five_now[ language]
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))