Untitled

 avatar
unknown
python
a year ago
4.9 kB
5
Indexable
allowed_channels(1015260651943383111, 1203433141205733376, error_message="Run the command in the channel: <#1015260651943383111>")

if server_var("riddle_game") == "True":
    set_error(f"❌ {user.mention}, A game is already in progress, please wait for the current game to finish")
else:
    update_server_var("riddle_game", "True")

db = load_database("clz8durud000kdvyvjsrhhuze")
riddles = db.find(where={"theme": theme})
riddles_total = 15

if len(riddles) >= riddles_total:
    riddles = pyrandom.sample(riddles, riddles_total)
else:
    riddles = riddles 

MAX_PASS = 3
MAX_ATT = 3
TIMEOUT = 60

CORRECT_PREFIX = "✅ Correct answer! Here is the next one:"
INCORRECT_MSG = "❌ Incorrect answer. Try again! Attempts remaining: `{}`"

PASS_PREFIX = "🔄 You have passed the riddle. Here is the next one:"
LAST_PASS_WARN = "⚠️ You can no longer pass, this is the last riddle. You must answer it."

END_TIMEOUT_MSG = "⏰ Time is up, {user}. **GAME OVER.**"
END_MAX_PASS_MSG = f"❌ You have reached the maximum number of passes (`{MAX_PASS}`). **GAME OVER.**"
END_MAX_ATT_MSG = f"❌ You have reached the maximum number of attempts (`{MAX_ATT}`). **GAME OVER.**"
CONGRATS_MSG = "## 🎉 Congratulations! You answered all the riddles correctly!"


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(interaction, user, channel):
    pyrandom.shuffle(riddles)
    respond_interaction(interaction, embed=load_embed("clyzurbw903hpcza7h4354w0p"))
    await send_riddle(channel)
    await handle_user_response(user, channel)

async def send_riddle(channel, 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(channel, CONGRATS_MSG)
            return
    
    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"Questions restantes : **`{remaining_riddles}`** | Passes utilisés : **`{passes_utilises}`** sur **`{MAX_PASS}`**"
        ),
        color=0xff0080
    )
    send_message(channel.id, message=Embed)
    return False

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

async def get_response(user, channel):
    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(channel, message):
    update_server_var("devinette", "False")
    send_message(channel.id, message)

async def process_response(user, channel, response):
    global current_riddle_index, attempts, passes, passed_riddles
    
    current_riddle = riddles[current_riddle_index]
    if response in ["passe", "p"]:
        await handle_pass(user, channel)
    elif response in current_riddle["answers"]:
        await handle_correct_response(user, channel)
    else:
        await handle_incorrect_response(user, channel)

async def handle_pass(user, channel):
    global current_riddle_index, attempts, passes, passed_riddles
    
    if passes >= MAX_PASS:
        await end_game(channel, END_MAX_PASS_MSG)
        return
    
    remaining_riddles, _ = get_remaining_info()
    if remaining_riddles == 0:
        send_message(channel.id, LAST_PASS_WARN)
    else:
        passes += 1
        passed_riddles.append(riddles[current_riddle_index])
        attempts = 0
        current_riddle_index += 1
        await send_riddle(channel, prefix=PASS_PREFIX)

    await handle_user_response(user, channel)

async def handle_correct_response(user, channel):
    global current_riddle_index, attempts
    
    current_riddle_index += 1
    attempts = 0
    if not await send_riddle(channel, prefix=CORRECT_PREFIX):
        await handle_user_response(user, channel)

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

await start_game(interaction, user, channel)

Editor is loading...
Leave a Comment