Untitled

mail@pastecode.io avatar
unknown
python
a month ago
4.5 kB
2
Indexable
Never
allowed_channels(1015260651943383111, 1203433141205733376, 
error_message="Exécuter la commande dans le salon: <#1015260651943383111>")

if server_var("devinette") == "True":
    set_error(f"❌ {user.mention}, Un jeu est déjà en cours")
else:
    update_server_var("devinette", "True")

RIDDLES_TOTAL = 15
MAX_PASS = 3
MAX_ATT = 3
TIMEOUT = 60
THRESHOLD = 75

CORRECT_MSG = "✅ Bonne réponse ! Prochaine devinette :"
INCORRECT_MSG = "❌ Mauvaise réponse. Réessayez ! Tentatives restantes : `{}`"

PASS_MSG = "🔄 Devinette passée. Suivante :"
PASS_WARN_MSG = "⚠️ Plus de passes disponibles ou c'est la dernière devinette."

END_TIMEOUT_MSG = f"⏰ Temps écoulé, {user.mention}. **GAME OVER.**"
END_MAX_ATT_MSG = f"❌ Limite de tentatives atteinte (`{MAX_ATT}`). **GAME OVER.**"
END_CONGRATS_MSG = "# 🎉 Bravo ! Vous avez terminé le jeu !"

db = load_database("clz8durud000kdvyvjsrhhuze")
riddles = db.find(where={"theme": theme})
riddles = pyrandom.sample(riddles, RIDDLES_TOTAL) if len(riddles) >= RIDDLES_TOTAL else riddles


########## The rest of the code goes in the post hook.

current_riddle_index = 0
attempts = 0
passes = 0
passed_riddles = []

def get_remaining_info():
    remaining_riddles = (len(riddles) - current_riddle_index - 1) + len(passed_riddles)
    return remaining_riddles, passes

async def start_game():
    pyrandom.shuffle(riddles)
    await send_riddle()
    await handle_user_response()

async def send_riddle(prefix="🧩 Première devinette:"):
    global current_riddle_index, riddles, passed_riddles
    
    if current_riddle_index >= len(riddles):
        if passed_riddles:
            riddles, passed_riddles = passed_riddles, []
            current_riddle_index = 0
        else:
            await end_game(END_CONGRATS_MSG)
            return False
    
    question = riddles[current_riddle_index]['question']
    remaining_riddles, passes_utilises = get_remaining_info()
    embed = Message(
        description=(
            f"{prefix}\n\n"
            f"❔ **{question}**\n\n"
            f"Thème: **`{theme}`**⠀Questions restantes: **`{remaining_riddles}`**⠀Passes utilisés: **`{passes_utilises}`** sur **`{MAX_PASS}`**"
        ),
        color=0xff0080
    )

    send_message(channel.id, message=embed)
    return True

async def handle_user_response():
    if current_riddle_index >= len(riddles) and not passed_riddles:
        return
    
    response = await get_response()
    if response:
        await process_response(response.content.lower())
    else:
        await end_game(END_TIMEOUT_MSG)

async def get_response():
    def predicate(event):
        return event.author_id == user.id and event.channel_id == channel.id
    
    return await wait_for("message", timeout=TIMEOUT, predicate=predicate)

async def end_game(message):
    update_server_var("devinette", "False")
    send_message(channel.id, message)

async def process_response(response):
    global current_riddle_index, attempts, passes, passed_riddles
    
    current_riddle = riddles[current_riddle_index]
    similarity = 0
    
    if response in ["passe", "pass", "p"]:
        await handle_pass()
        return
    
    def checker(correct_ans, index):
        nonlocal similarity
        result = fuzz.ratio(response, correct_ans.lower())
        similarity = max(similarity, result)

    loop(current_riddle["answers"], checker)

    if similarity >= THRESHOLD:
        await handle_correct_response()
    else:
        await handle_incorrect_response()

async def handle_pass():
    global current_riddle_index, attempts, passes, passed_riddles
    remaining_riddles, _ = get_remaining_info()
    
    if passes >= MAX_PASS or remaining_riddles == 0:
        send_message(channel.id, PASS_WARN_MSG)
    else:
        passes += 1
        passed_riddles.append(riddles[current_riddle_index])
        attempts = 0
        current_riddle_index += 1
        await send_riddle(prefix=PASS_MSG)
    
    await handle_user_response()

async def handle_correct_response():
    global current_riddle_index, attempts
    
    current_riddle_index += 1
    attempts = 0
    if await send_riddle(prefix=CORRECT_MSG):
        await handle_user_response()

async def handle_incorrect_response():
    global attempts
    
    attempts += 1
    if attempts >= MAX_ATT:
        await end_game(END_MAX_ATT_MSG)
    else:
        send_message(channel.id, INCORRECT_MSG.format(MAX_ATT - attempts))
        await handle_user_response()

await start_game()
Leave a Comment