Ejemplo n.º 1
0
 def wrapped(update, context, *args, **kwargs):
     chat = get_chat(update.effective_chat)
     if DEBUG or chat.id in ALLOWED_CHATS_INTERNAL + ALLOWED_CHATS_EXTERNAL:
         return func(update, context, *args, **kwargs)
     else:
         update.message.reply_text(
             f"Your chat id is {chat.id}.\nShare it with bot owner to be authorized."
         )
Ejemplo n.º 2
0
def hour_add(update, context):
    query = update.callback_query
    hour = int(re.search("hour_add_(.*)", query.data).group(1))
    chat = get_chat(update.effective_chat)
    chat.add_hour(hour)
    query.answer()
    query.edit_message_text(text=f"{hour:02d}:00 added")
    return ConversationHandler.END
Ejemplo n.º 3
0
def weekday_add(update, context):
    query = update.callback_query
    weekday = re.search("weekday_add_(.*)", query.data).group(1)
    chat = get_chat(update.effective_chat)
    chat.add_day_off(weekday)
    query.answer()
    query.edit_message_text(text=f"{weekday} added")
    return ConversationHandler.END
Ejemplo n.º 4
0
def set_chat_timezone(update, context):
    query = update.callback_query
    new_tz = re.search("TZ_chat_(.*)", query.data).group(1)
    chat = get_chat(update.effective_chat)
    chat.timezone = new_tz
    chat.save()
    query.answer()
    query.edit_message_text(text=f"Your chat's TZ was set to {new_tz}")
    return MAIN_STATE
Ejemplo n.º 5
0
def set_days_off(update, context):
    query = update.callback_query
    chat = get_chat(update.effective_chat)
    keyboard = [[
        InlineKeyboardButton(f"{Emoji.check} {weekday}",
                             callback_data=f"weekday_rm_{weekday}")
    ] if weekday in chat.days_off else [
        InlineKeyboardButton(weekday, callback_data=f"weekday_add_{weekday}")
    ] for weekday in WEEKDAYS]
    query.answer()
    query.edit_message_text(
        text=f"Pick days off",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return MAIN_STATE
Ejemplo n.º 6
0
def chat_timezone(update, context):
    """Set current user's timezone"""
    query = update.callback_query
    chat = get_chat(update.effective_chat)
    keyboard = [
        [
            InlineKeyboardButton(f"{Emoji.check} {tz}, {code}",
                                 callback_data=f"TZ_chat_{tz}")
        ] if tz == chat.timezone else
        [InlineKeyboardButton(f"{tz}, {code}", callback_data=f"TZ_chat_{tz}")]
        for tz, code in COMMON_TIMEZONES.items()
    ]
    query.answer()
    query.edit_message_text(
        text=f"Your chat's timezone is {chat.timezone}\nSet new timezone:",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return MAIN_STATE
Ejemplo n.º 7
0
def hours_keyboard(update):
    """Returns keyboard with timeslots for new game"""
    player = get_player(update)
    chat = get_chat(update.effective_chat)
    timezone = player.timezone_pytz
    main_hours_dt = [
        convert_to_dt(timeslot=f"{hour:02d}:00", timezone=timezone)
        for hour in chat.main_hours
    ]
    ts_games = get_all_games(update, ts_only=True)
    ts_filtered = [
        timeslot.astimezone(timezone).strftime("%H:%M")
        for timeslot in main_hours_dt
        if timeslot not in ts_games and timeslot > dt.now(timezone)
    ]
    keyboard = [
        InlineKeyboardButton(timeslot_time, callback_data=timeslot_time)
        for timeslot_time in ts_filtered
    ]
    return row_list_chunks(keyboard)
Ejemplo n.º 8
0
def set_game_hours(update, context):
    query = update.callback_query
    chat = get_chat(update.effective_chat)
    keyboard = [[
        InlineKeyboardButton(
            f"{Emoji.check} {hour:02d}:00",
            callback_data=f"hour_rm_{hour}",
        )
    ] if hour in chat.main_hours else [
        InlineKeyboardButton(
            f"{hour:02d}:00",
            callback_data=f"hour_add_{hour}",
        )
    ] for hour in EXTENDED_HOURS]
    query.answer()
    query.edit_message_text(
        text=f"Pick hours",
        reply_markup=InlineKeyboardMarkup(keyboard),
    )
    return MAIN_STATE
Ejemplo n.º 9
0
def in_out(update, context, action, hard_args=None):
    """Ugliest function of them all"""
    args = hard_args if hard_args else context.args
    chat = get_chat(update.effective_chat)
    if args:
        player = get_player(update)
        if args[0].lower() == "all":
            for game in get_all_games(update):
                if action == "in" and player not in game.players:
                    game.add_player(player, joined_at=dt.now(pytz.utc))

                elif action == "out" and player in game.players:
                    remove_player_and_clean_game(context, game, player)
        else:
            filtered_args = expand_hours(
                chat=chat,
                hours_list=[
                    argv for argv in args if re.search("^[0-9]+-[0-9]+$", argv)
                    or re.search("^[0-9]+$", argv)
                ],
            )
            for argv in filtered_args:
                timeslot = convert_to_dt(
                    timeslot=f"{int(argv):02d}:00",
                    timezone=player.timezone_pytz,
                )
                game = get_game(chat_id=chat.id, timeslot=timeslot)

                if action == "in":
                    if not game and timeslot > dt.now(pytz.utc):
                        create_game_and_add_player(update, context, player,
                                                   timeslot)
                    elif game and player not in game.players:
                        game.add_player(player, joined_at=dt.now(pytz.utc))

                elif action == "out":
                    if game and player in game.players:
                        remove_player_and_clean_game(context, game, player)
Ejemplo n.º 10
0
 def wrapped(update, context, *args, **kwargs):
     chat = get_chat(update.effective_chat)
     if is_dayoff(chat):
         return dayoff(update, context)
     else:
         return func(update, context, *args, **kwargs)