Ejemplo n.º 1
0
def comment_text(update, context):
    """
    MessageHandler callback for processing comments sent by a user.
    Notifies the admins of the bot about the comment
    """
    # Only handle the message, if the user is currently in the "commenting" state
    if context.user_data.get("state", None) != UserState.COMMENTING:
        return

    user = update.effective_user
    chat = update.effective_chat
    lang_id = Database().get_lang_id(chat.id)

    # username can be None, so we need to use str()
    data = [
        chat.id, user.id, user.first_name, user.last_name,
        "@" + str(user.username), user.language_code
    ]

    userdata = " | ".join([str(item) for item in data])
    userdata = userdata.replace("\r", "").replace("\n", "")

    text = update.effective_message.text

    notify_admins(
        "New comment from a user:\n\n{}\n\n{}".format(text, userdata), context)
    update.message.reply_text(translate("received_comment", lang_id))

    context.user_data["state"] = UserState.IDLE
Ejemplo n.º 2
0
def get_user_stats(user_id):
    """
    Generates and returns a string displaying the statistics of a user
    :param user_id: The user_id of a specific user
    :return:
    """
    user = Database().get_user(user_id)

    if user is None:
        logger.warning("User '{}' is not stored in the database!".format(user_id))
        return "No statistics found!"

    lang_id = Database().get_lang_id(user_id)

    try:
        played_games, won_games, _, last_played = user[4:8]
    except ValueError as e:
        logger.warning("Cannot unpack user - {}".format(e))
        raise

    if played_games == 0:
        # prevent division by zero errors
        played_games = 1

    last_played_formatted = datetime.utcfromtimestamp(last_played).strftime('%d.%m.%y %H:%M')
    win_percentage = round(float(won_games) / float(played_games), 4)
    bar = generate_bar_chart(win_percentage * 100)
    template = translate("statistic_template", lang_id)
    statistics_string = template.format(played_games, won_games, last_played_formatted, bar, win_percentage)
    return statistics_string
Ejemplo n.º 3
0
def get_card_string(card, lang_id):
    """Returns the translated string representation of a card object"""
    if card.type == Card.Type.NUMBER:
        face = card.value
    else:
        face = translate(card.str_id, lang_code=lang_id)

    return "{} {}".format(card.symbol, face)
Ejemplo n.º 4
0
def comment_cmd(update, context):
    """MessageHandler callback for the /comment command"""
    if context.user_data.get("state", UserState.IDLE) != UserState.IDLE:
        return

    chat = update.effective_chat
    lang_id = Database().get_lang_id(chat.id)
    update.message.reply_text(translate("send_comment", lang_id),
                              reply_markup=ForceReply())
    context.user_data["state"] = UserState.COMMENTING
Ejemplo n.º 5
0
def language_callback(update, context):
    """
    Callback function to handle inline buttons of the /language menu for changing the language
    """
    query_data = update.callback_query.data
    lang_id = re.search(r"^lang_([a-z]{2}(?:-[a-z]{2})?)$", query_data).group(1)

    # Inform user about language change
    lang = get_language_info(lang_id)
    lang_changed_text = translate("lang_changed", lang_id).format(lang.get("display_name"))
    update.effective_message.edit_text(text=lang_changed_text, reply_markup=None)

    Database().set_lang_id(lang_id=lang_id, chat_id=update.effective_chat.id)
    logger.debug("Language changed to '{}' for user {}".format(lang_id, update.effective_user.id))
Ejemplo n.º 6
0
def language_cmd(update, context):
    """
    Handler for /language commands
    """
    buttons = []

    for lang in get_available_languages():
        display_name = lang.get("display_name")
        lang_code = lang.get("lang_code")
        buttons.append(InlineKeyboardButton(text=display_name, callback_data="lang_{}".format(lang_code)))

    lang_keyboard = InlineKeyboardMarkup(build_menu(buttons, n_cols=3))

    lang_id = Database().get_lang_id(update.effective_chat.id)
    update.message.reply_text(text=translate("select_lang", lang_id), reply_markup=lang_keyboard)