return False
    key_info = keys[user_key]
    if key_info.get('expired', False):
        return False
    expiry_date = datetime.fromisoformat(key_info['expires_at'])
    if datetime.now() > expiry_date:
        return False
    return True
def redeem_key(user_id, key):
    keys = load_keys()
    users = load_users()
    user_id_str = str(user_id)
    if user_id_str in users:
        user_key = users[user_id_str].get('redeemed_key')
        if user_key and user_key in keys:
            key_info = keys[user_key]
            if not key_info.get('expired', False):
                expiry_date = datetime.fromisoformat(key_info['expires_at'])
                if datetime.now() < expiry_date:
                    return False, "You already have an active key. Cannot redeem another until it expires."
    if key not in keys:
        return False, "Invalid key."
    if keys[key].get('redeemed', False):
        return False, "This key has already been redeemed."
    keys[key]['redeemed'] = True
    keys[key]['redeemed_by'] = user_id_str
    keys[key]['redeemed_at'] = datetime.now().isoformat()
    users[user_id_str] = {
        'redeemed_key': key,
        'redeemed_at': datetime.now().isoformat()
    }
    save_keys(keys)
    save_users(users)
    return True, "Key successfully redeemed!"
def search_mongodb(query):
    try:
        collection.create_index([("url", "text"), ("username", "text")])
        pattern = {'$regex': query, '$options': 'i'}
        search_criteria = {
            '$or': [
                {'url': pattern},
                {'username': pattern}
            ]
        }
        projection = {
            '_id': 0,
            'url': 1,
            'username': 1,
            'password': 1
        }
        pipeline = [
            {'$match': search_criteria},
            {'$project': projection},
            {'$sample': {'size': 200}}
        ]
        results = list(collection.aggregate(
            pipeline,
            allowDiskUse=True
        ))
        if not results:
            return None
        formatted_results = set() 
        batch_size = 50
        for i in range(0, len(results), batch_size):
            batch = results[i:i + batch_size]
            for result in batch:
                if all(key in result for key in ['url', 'username', 'password']):
                    formatted_line = f"{result['url']}:{result['username']}:{result['password']}"
                    formatted_results.add(formatted_line)
            if i + batch_size < len(results):
                time.sleep(0.1)
        if formatted_results:
            final_results = list(formatted_results)
            random.shuffle(final_results)
            return '\n'.join(final_results[:200]) 
        return None
    except PyMongoError as e:
        print(f"MongoDB Error: {e}")
        return None
    except Exception as e:
        print(f"Error in search: {e}")
        return None
    except PyMongoError as e:
        print(f"MongoDB Error: {e}")
        return None
@bot.message_handler(commands=['start'])
def start_command(message):
    user_id = str(message.from_user.id)
    if not is_user_authorized(user_id):
        bot.send_message(message.chat.id, 
            "👋 Welcome to Logs Generator Bot!\n\n"
            "🔐 You need to redeem a key to use this bot\n"
            "📝 Use /redeem to activate your access\n\n"
            "💡 Need a key? Contact @anetjaka"
        )
        return
def count_active_users():
    keys = load_keys()
    current_time = datetime.now()
    active_users_count = 0