Untitled

mail@pastecode.io avatar
unknown
python
5 months ago
3.8 kB
1
Indexable
passed_riddles = []
ALLOW_PASSING = True

RIDDLE_TEMPLATE = (
    "{prefix}\n\n"
    "❔ **{question}**\n"
    f"Theme: **{theme}** - "
    "Questions Remaining: **{remaining}** - "
    "Passes Utilized: **{passes_utilises}** "
    f"out of **`{MAX_PASS}`**"
)

async def is_correct(riddle, user_input):
    similarity = 0

    def checker(correct_ans, index):
        nonlocal similarity
        result = fuzz.ratio(user_input, correct_ans.lower())
        similarity = max(similarity, result)

    loop(riddle["answers"], checker)
    return similarity >= THRESHOLD

async def handle_response(i, index, riddle, riddle_index):
    global passed_riddles
    def checker(event):
        return event.author_id == user.id and event.channel_id == channel.id
    
    event = await wait_for("message", timeout=10, check=checker)

    if not event:
        return break_loop("TIMED_OUT")
    
    delete_message(event.channel_id, event.message.id)

    if event.message.content in ["pass", "p"]:
        if ALLOW_PASSING:
            if PASSED == MAX_PASS:
                respond_interaction(
                    interaction, 
                    content=RIDDLE_TEMPLATE.format(
                        prefix=PASS_WARN_MSG,
                        question=riddle["question"],
                        remaining=len(riddles) - (riddle_index + 1),
                        passes_utilises=PASSED,
                    )
                )
                return iterate_again()
            else:
                passed_riddles.append(riddle)
        
            return break_loop("PASSED")
        else:
            respond_interaction(
                interaction, 
                content=RIDDLE_TEMPLATE.format(
                    prefix="Passing is not allowed",
                    question=riddle["question"],
                    remaining=len(riddles) - (riddle_index + 1),
                    passes_utilises=PASSED,
                )
            )
            return iterate_again()
    
    if await is_correct(riddle, event.message.content):
        return break_loop("CORRECT")
    
    respond_interaction(
        interaction, 
        content=RIDDLE_TEMPLATE.format(
            prefix=INCORRECT_MSG.format(remaining_attempts=MAX_ATT - (index + 1)),
            question=riddle["question"],
            remaining=len(riddles) - (riddle_index + 1),
            passes_utilises=PASSED,
        )
    )


async def send_riddle(riddle, index, prefix = ""):
    global PASSED, CURRENT_PREFIX

    respond_interaction(
        interaction, 
        content=RIDDLE_TEMPLATE.format(
            prefix=CURRENT_PREFIX,
            question=riddle["question"],
            remaining=len(riddles) - (index + 1),
            passes_utilises=PASSED,
        )
    )

    data = loop(3, handle_response, riddle=riddle, riddle_index=index)

    if data == "PASSED":
        PASSED += 1
    
    if data in ["TIMED_OUT", "PASSED", "CORRECT"]:
        return data

    # gave wrong answer 3 times
    return "INCORRECT"

async def handle_game(riddle, index):
    global final_response, CURRENT_PREFIX

    result = await send_riddle(riddle, index, prefix=CURRENT_PREFIX)

    if result == "TIMED_OUT":
        final_response = END_TIMEOUT_MSG
        return break_loop()
    
    elif result == "CORRECT":
        CURRENT_PREFIX = CORRECT_MSG
        return
    
    elif result == "PASSED":
        CURRENT_PREFIX = PASS_MSG
        return
    
    elif result == "INCORRECT":
        final_response = INCORRECT_MSG.format(remaining_attempts=0)
        return break_loop()
        # you might wanted to format this message

loop(riddles, handle_game)
ALLOW_PASSING = False
loop(passed_riddles, handle_game)
Leave a Comment