def handle(update, context): util.log_chat("reset", update) try: # Extract query... query = update.message.text query = query.split(" ") target = None if not util.is_admin(update.message.from_user["id"]): return # Request includes the username as a mention if update.message.entities: for entity in update.message.entities: if entity.type == "text_mention" and entity.user is not None: target = dao.get_bakchod_by_id(entity.user.id) # Last attempt... try to lookup username in DB if target is None: receiver_username = query[1] # Remove the "@" prefix if receiver_username.startswith("@"): receiver_username = receiver_username[1:] target = dao.get_bakchod_by_username(receiver_username) if target is not None: logger.info("[reset] target={}", util.extract_pretty_name_from_bakchod(target)) target.modifiers = {} dao.insert_bakchod(target) update.message.reply_text( "Reset - " + util.extract_pretty_name_from_bakchod(target)) return except Exception as e: logger.error( "Caught Error in reset.handle - {} \n {}", e, traceback.format_exc(), )
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
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(), )
def handle(update, context): try: util.log_chat("daan", update) # Extract query... query = update.message.text query = query.split(" ") if len(query) < 2: update.message.reply_text( text="Haat chutiya! Syntax is `/daan @username 786`", parse_mode=ParseMode.MARKDOWN, ) return # Extract Sender by ID sender = dao.get_bakchod_by_id(update.message.from_user["id"]) if sender is None: sender = Bakchod.fromUpdate(update) dao.insert_bakchod(sender) # Extract Receiver receiver = None if update.message.reply_to_message: # Request is a reply to message... Extract receiver from ID receiver = dao.get_bakchod_by_id( update.message.reply_to_message.from_user["id"] ) # Donation can be the rest of the message donation = query[1:] else: # Request includes the username as a mention if update.message.entities: for entity in update.message.entities: if entity.type == "text_mention" and entity.user is not None: receiver = dao.get_bakchod_by_id(entity.user.id) # Last attempt... try to lookup username in DB if receiver is None: receiver_username = query[1] # Remove the "@" prefix if receiver_username.startswith("@"): receiver_username = receiver_username[1:] receiver = dao.get_bakchod_by_username(receiver_username) # Donation can be the rest of the message donation = query[2:] # Handle if receiver could be extracted if receiver is None: if receiver_username: update.message.reply_text(receiver_username + "??? Who dat???") return else: update.message.reply_text("Kisko daan do be????") return # Parse Daan amount try: daan = float("".join(donation)) daan = round(daan, 2) daan = abs(daan) except Exception as e: update.message.reply_text("Kitna ₹okda be???") return logger.info( "[daan] sender={} receiver={} daan={}", util.extract_pretty_name_from_bakchod(sender), util.extract_pretty_name_from_bakchod(receiver), daan, ) if (sender.rokda - daan) < 0: update.message.reply_text("Gareeb saale! You don't have enough ₹okda!") return if sender.id == receiver.id: file_id = "CAADAwADrQADnozgCI_qxocBgD_OFgQ" sticker_to_send = file_id update.message.reply_sticker(sticker=sticker_to_send) return # Commit Daan transaction to DB sender.rokda = sender.rokda - daan dao.insert_bakchod(sender) receiver.rokda = receiver.rokda + daan dao.insert_bakchod(receiver) daan_id = shortuuid.uuid() dao.insert_daan( daan_id, sender.id, receiver.id, daan, str(datetime.datetime.now()) ) update.message.reply_text( text="{} gave {} 🤲 a daan of {} ₹okda! 🎉 \n Daan ID - <code>{}</code>".format( util.extract_pretty_name_from_bakchod(sender), util.extract_pretty_name_from_bakchod(receiver), daan, daan_id, ), parse_mode=ParseMode.HTML, ) return except Exception as e: logger.error( "Caught Error in daan.handle - {} \n {}", e, traceback.format_exc(), )