Esempio n. 1
0
def reset_roll_effects(context: telegram.ext.CallbackContext):

    # Get group_id
    group_id = context.job.context

    # Get relevant roll based on group_id
    roll = dao.get_roll_by_id(group_id)

    # Get victim's Bakchod
    victim = dao.get_bakchod_by_id(roll["victim"])

    logger.info(
        "[roll] Resetting roll effects for {} in group={}",
        util.extract_pretty_name_from_bakchod(victim),
        group_id,
    )

    # Reset victim's modifiers
    if roll["rule"] == "mute_user":
        censored_modifiers = victim.modifiers["censored"]
        if censored_modifiers is not None:
            censored_modifiers["group_ids"].remove(group_id)
            victim.modifiers["censored"] = censored_modifiers

    elif roll["rule"] == "auto_mom":
        auto_mom_modifiers = victim.modifiers["auto_mom"]
        if auto_mom_modifiers is not None:
            auto_mom_modifiers["group_ids"].remove(group_id)
            victim.modifiers["auto_mom"] = auto_mom_modifiers

    dao.insert_bakchod(victim)

    # Post reset message
    response = "Roll Modifiers for {} are now removed!".format(
        util.extract_pretty_name_from_bakchod(victim)
    )
    context.bot.send_message(
        group_id, text=response,
    )

    return
Esempio n. 2
0
def start_new_daily_roll(context: telegram.ext.CallbackContext):

    for group_id in chaddi_config["good_morning_channels"]:

        logger.info("[roll] starting new daily roll! group_id={}", group_id)

        # Set roll rule type
        rule = get_random_roll_type()

        # Set the number to required to win
        roll_number = random.randint(1, 6)

        victim = get_random_bakchod(group_id)
        if victim is None:
            context.bot.send_message(chat_id=group_id,
                                     text="Couldn't get a random Bakchod :(")
            return

        winrar = None

        expiry = datetime.datetime.now() + datetime.timedelta(hours=1)

        dao.insert_roll(group_id, rule, roll_number, victim.id, winrar, expiry)

        roll = dao.get_roll_by_id(group_id)

        if roll is None:
            context.bot.send_message(
                chat_id=group_id,
                text="bhaaaaaaaaaaaaak",
                parse_mode=telegram.ParseMode.HTML,
            )

        pretty_roll = generate_pretty_roll(roll)
        response = "<b>Started new roulette!</b> " + pretty_roll

        context.bot.send_message(
            chat_id=group_id,
            text=response,
            parse_mode=telegram.ParseMode.HTML,
        )
Esempio n. 3
0
def generate_new_roll(update, group_id):

    roll = Roll()

    params = extract_params_from_update(update)

    if len(params) >= 4:

        param_rule = params[2]
        if param_rule in ROLL_TYPES:
            roll.rule = param_rule

        param_victim_username = params[3]
        victim = dao.get_bakchod_by_username(param_victim_username)
        if victim is not None:
            roll.victim_id = victim.id

    if roll.rule is None:
        roll.rule = get_random_roll_type()

    if roll.roll_number is None:
        roll.roll_number = random.randint(1, 6)

    if roll.victim_id is None:
        victim = get_random_bakchod(group_id)
        roll.victim_id = victim.id

    if roll.expiry is None:
        roll.expiry = datetime.datetime.now() + datetime.timedelta(hours=1)

    logger.debug("[roll] generated roll={}", roll.__dict__)

    dao.insert_roll(
        group_id, roll.rule, roll.roll_number, roll.victim_id, None, roll.expiry
    )

    roll = dao.get_roll_by_id(group_id)

    return roll
Esempio n. 4
0
def handle(update, context):

    util.log_chat("roll", update)

    try:
        command = extract_command_from_update(update)

        if command == "start":

            create_new_roll = False

            group_id = get_group_id_from_update(update)
            if group_id is None:
                update.message.reply_text(text="Roll only be used in a group!")
                return

            current_roll = dao.get_roll_by_id(group_id)
            if current_roll is None:
                create_new_roll = True
            else:
                # Pre-exisitng roll... check if expired
                roll_expiry = ciso8601.parse_datetime(current_roll["expiry"])
                if roll_expiry <= datetime.datetime.now():
                    create_new_roll = True

            # Allow admins to create new rolls
            if util.is_admin(update.message.from_user["id"]):
                create_new_roll = True

            if create_new_roll:

                roll = generate_new_roll(update, group_id)
                if roll is None:
                    update.message.reply_text(text="Couldn't start a new roll :(")
                    return

                metrics.rolls_started.inc()

                pretty_roll = generate_pretty_roll(roll)
                response = "<b>Started new roulette!</b> " + pretty_roll
                update.message.reply_text(
                    text=response, parse_mode=telegram.ParseMode.HTML
                )
                return

            else:
                # Handle when not create_new_roll
                update.message.reply_text("Chal kat re bsdk!")
                return

        elif command == "reset":

            if util.is_admin(update.message.from_user["id"]):

                # Remove schedule reset job
                for job in context.job_queue.jobs():
                    if (
                        job.name == "reset_roll_effects"
                        and job.context == update.message.chat_id
                    ):
                        logger.info(
                            "[roll] Removing pre-scheduled reset_roll_effects job..."
                        )
                        job.schedule_removal()

                # Schedule callback for resetting roll effects
                context.job_queue.run_once(
                    reset_roll_effects, 1, context=update.message.chat_id
                )
                return

            else:
                update.message.reply_text("Chal kat re bsdk!")
                return

        else:

            # Return roll status for anything else

            group_id = get_group_id_from_update(update)
            if group_id is None:
                update.message.reply_text(text="Roll can only be used in a group!")
                return

            current_roll = dao.get_roll_by_id(group_id)

            if current_roll is None:
                update.message.reply_text(
                    text="No active roulette right now. <code>/roll start</code> to start one!",
                    parse_mode=telegram.ParseMode.HTML,
                )
                return
            else:
                # Handle expired roll
                roll_expiry = ciso8601.parse_datetime(current_roll["expiry"])
                if roll_expiry <= datetime.datetime.now():
                    update.message.reply_text(
                        text="No active roulette right now. <code>/roll start</code> to start one!",
                        parse_mode=telegram.ParseMode.HTML,
                    )
                    return
                else:
                    pretty_roll = generate_pretty_roll(current_roll)
                    response = "<b>Current active roulette:</b> {}".format(pretty_roll)
                    update.message.reply_text(
                        text=response, parse_mode=telegram.ParseMode.HTML
                    )
                    return

    except Exception as e:
        logger.error(
            "Caught Error in roll.handle - {} \n {}", e, traceback.format_exc(),
        )
Esempio n. 5
0
def handle_dice_rolls(dice_value, update, context):

    try:

        metrics.rolls_attempted.inc()

        logger.info("[roll] handle_dice_rolls dice_value={}", dice_value)

        # Extract Group ID
        group_id = get_group_id_from_update(update)
        if group_id is None:
            logger.info("[roll] dice roll was not in a group... skipping")
            return

        # Get current roll based on group_id
        current_roll = dao.get_roll_by_id(group_id)
        if current_roll is None:
            logger.info("[roll] current_roll was a None... skipping")
            return

        # Check whether roll already a winrar
        if current_roll["winrar"] is not None:
            logger.info("[roll] current_roll already has a winrar... skipping")
            return

        # Check roll expiry
        roll_expiry = ciso8601.parse_datetime(current_roll["expiry"])
        if roll_expiry <= datetime.datetime.now():
            logger.info("[roll] current_roll has expired... skipping")
            return

        # Check and update roll history
        roller = dao.get_bakchod_by_id(update.message.from_user.id)
        history = roller.history

        if history is None:
            history = {}

        five_min_ago = datetime.datetime.now() - datetime.timedelta(minutes=5)

        if not DEBUG:
            if "roll" in history:
                last_time_rolled = ciso8601.parse_datetime(history["roll"])
                if last_time_rolled > five_min_ago:
                    logger.info("[roll] rolled too soon... skipping")
                    update.message.reply_text(
                        "You can only roll once every 5 mins... Ignoring this roll!"
                    )
                    return
        else:
            logger.debug("[roll] DEBUG is True... ignoring time check")

        history["roll"] = datetime.datetime.now()
        roller.history = history
        dao.insert_bakchod(roller)

        # Check roll outcome
        roll_number = int(current_roll["roll_number"])

        if dice_value == roll_number:

            metrics.rolls_won.inc()

            # Handle roll winrar
            winrar_bakchod = dao.get_bakchod_by_id(update.message.from_user.id)

            logger.info(
                "[roll] We got a winrar! bakchod={} roll_number={} group_id={}",
                util.extract_pretty_name_from_bakchod(winrar_bakchod),
                roll_number,
                group_id,
            )

            current_roll["winrar"] = winrar_bakchod.id
            current_roll["expiry"] = datetime.datetime.now() + datetime.timedelta(
                hours=1
            )

            # Update roll in DB
            dao.insert_roll(
                current_roll["group_id"],
                current_roll["rule"],
                current_roll["roll_number"],
                current_roll["victim"],
                current_roll["winrar"],
                current_roll["expiry"],
            )

            # Add roll effect to victims modifiers
            victim = dao.get_bakchod_by_id(current_roll["victim"])
            modifiers = victim.modifiers

            if current_roll["rule"] == "mute_user":

                if "censored" in modifiers:
                    censored_modifier = modifiers["censored"]
                else:
                    censored_modifier = {}

                if "group_ids" not in censored_modifier:
                    censored_modifier["group_ids"] = []

                censored_modifier["group_ids"].append(group_id)

                modifiers["censored"] = censored_modifier

            elif current_roll["rule"] == "auto_mom":

                if "auto_mom" in modifiers:
                    auto_mom_modifier = modifiers["auto_mom"]
                else:
                    auto_mom_modifier = {}

                if "group_ids" not in auto_mom_modifier:
                    auto_mom_modifier["group_ids"] = []

                auto_mom_modifier["group_ids"].append(group_id)

                modifiers["auto_mom"] = auto_mom_modifier

            elif current_roll["rule"] == "kick_user":

                try:

                    logger.info(
                        "[roll] Kicking User - group_id={} victim.id={}",
                        group_id,
                        victim.id,
                    )
                    context.bot.send_message(
                        chat_id=update.message.chat_id,
                        text="BYEEEEEEEEEEEE "
                        + util.extract_pretty_name_from_bakchod(victim),
                    )

                    # refer to https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html#telegram.Bot.kick_chat_member
                    ban_until = datetime.datetime.now() + datetime.timedelta(seconds=31)
                    context.bot.kick_chat_member(
                        chat_id=group_id, user_id=victim.id, until_date=ban_until
                    )

                    # remove the victim from group members list
                    group = dao.get_group_by_id(group_id)
                    group.members.remove(victim.id)
                    dao.insert_group(group)

                except Exception as e:
                    logger.error(
                        "[roll] Caught Error in kick Bakchod - {} \n {}",
                        e,
                        traceback.format_exc(),
                    )
                    context.bot.send_message(
                        chat_id=update.message.chat_id,
                        text="Looks like I'm not able to kick user... Please check the Group permissions!",
                    )
                    return

            victim.modifiers = modifiers
            dao.insert_bakchod(victim)

            response = "<b>WINRAR!!!</b> {}".format(generate_pretty_roll(current_roll))
            update.message.reply_text(text=response, parse_mode=telegram.ParseMode.HTML)

            # Schedule callback for resetting roll effects in 1 hour
            context.job_queue.run_once(
                reset_roll_effects, 3600, context=update.message.chat_id
            )

    except Exception as e:
        logger.error(
            "Caught Error in roll.handle_dice_rolls - {} \n {}",
            e,
            traceback.format_exc(),
        )

    return
Esempio n. 6
0
def handle(update, context):

    util.log_chat("roll", update)

    try:
        command = extract_command_from_update(update)

        if command == "start":

            create_new_roll = False

            group_id = get_group_id_from_update(update)
            if group_id is None:
                update.message.reply_text(text="Roll only be used in a group!")
                return

            current_roll = dao.get_roll_by_id(group_id)
            if current_roll is None:
                create_new_roll = True
            else:
                # Pre-exisitng roll... check if expired
                roll_expiry = ciso8601.parse_datetime(current_roll["expiry"])
                if roll_expiry <= datetime.datetime.now():
                    create_new_roll = True

            # Allow admins to create new rolls
            if util.is_admin(update.message.from_user["id"]):
                create_new_roll = True

            if create_new_roll:

                # Create new roll...

                # Set roll rule type
                rule = get_random_roll_type()

                # Set the number to required to win
                roll_number = random.randint(1, 6)

                # Extract victim from command param...
                victim = None
                parsed_victim_username = extract_param_from_update(update)
                if parsed_victim_username is not None:
                    victim = dao.get_bakchod_by_username(
                        parsed_victim_username)
                    if victim is not None:
                        logger.info(
                            "[roll] Found passed Bakchod username={} in DB",
                            parsed_victim_username,
                        )

                # Or else get a Random Bakchod
                if victim is None:
                    victim = get_random_bakchod(group_id)

                if victim is None:
                    update.message.reply_text(
                        text="Couldn't get a random Bakchod :(")
                    return

                # Set the roll winrar to None
                winrar = None

                expiry = datetime.datetime.now() + datetime.timedelta(hours=1)

                dao.insert_roll(group_id, rule, roll_number, victim.id, winrar,
                                expiry)

                roll = dao.get_roll_by_id(group_id)
                if roll is None:
                    update.message.reply_text(
                        text="Couldn't start a new roll :(")
                    return

                pretty_roll = generate_pretty_roll(roll)
                response = "<b>Started new roulette!</b> " + pretty_roll
                update.message.reply_text(text=response,
                                          parse_mode=telegram.ParseMode.HTML)
                return

            else:
                # Handle when not create_new_roll
                update.message.reply_text("Chal kat re bsdk!")
                return

        elif command == "reset":

            if util.is_admin(update.message.from_user["id"]):

                # Remove schedule reset job
                for job in context.job_queue.jobs():
                    if (job.name == "reset_roll_effects"
                            and job.context == update.message.chat_id):
                        logger.info(
                            "[roll] Removing pre-scheduled reset_roll_effects job..."
                        )
                        job.schedule_removal()

                # Schedule callback for resetting roll effects
                context.job_queue.run_once(reset_roll_effects,
                                           1,
                                           context=update.message.chat_id)
                return

            else:
                update.message.reply_text("Chal kat re bsdk!")
                return

        else:

            # Return roll status for anything else

            group_id = get_group_id_from_update(update)
            if group_id is None:
                update.message.reply_text(
                    text="Roll can only be used in a group!")
                return

            current_roll = dao.get_roll_by_id(group_id)

            if current_roll is None:
                update.message.reply_text(
                    text=
                    "No active roulette right now. <code>/roll start</code> to start one!",
                    parse_mode=telegram.ParseMode.HTML,
                )
                return
            else:
                # Handle expired roll
                roll_expiry = ciso8601.parse_datetime(current_roll["expiry"])
                if roll_expiry <= datetime.datetime.now():
                    update.message.reply_text(
                        text=
                        "No active roulette right now. <code>/roll start</code> to start one!",
                        parse_mode=telegram.ParseMode.HTML,
                    )
                    return
                else:
                    pretty_roll = generate_pretty_roll(current_roll)
                    response = "<b>Current active roulette:</b> {}".format(
                        pretty_roll)
                    update.message.reply_text(
                        text=response, parse_mode=telegram.ParseMode.HTML)
                    return

    except Exception as e:
        logger.error(
            "Caught Error in roll.handle - {} \n {}",
            e,
            traceback.format_exc(),
        )