Untitled
unknown
python
a year ago
5.0 kB
7
Indexable
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") 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 = "✅ Bonne réponse ! Prochaine devinette :" INCORRECT_MSG = "❌ Mauvaise réponse. Réessayez ! Tentatives restantes : `{}`" PASS_PREFIX = "🔄 Devinette passée. Suivante :" LAST_PASS_WARN = "⚠️ Plus de passes disponibles ou c'est la derniére devinette." END_TIMEOUT_MSG = f"⏰ Temps écoulé, {user.mention}. **JEU TERMINÉ.**" END_MAX_ATT_MSG = f"❌ Limite de tentatives atteinte (`{MAX_ATT}`). **JEU TERMINÉ.**" CONGRATS_MSG = "## 🎉 Bravo ! Vous avez terminé le jeu !" set_error_log_channel(1203433141205733376) 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) 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) 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] answers = current_riddle["answers"] similarity = 0 threshold = 80 def checker(correct_ans, index): nonlocal similarity result = fuzz.ratio(response, correct_ans.lower()) similarity = max(similarity, result) loop(answers, checker) is_good_match = similarity >= threshold if response in ["passe", "p"]: await handle_pass(user, channel) elif is_good_match: 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 remaining_riddles, _ = get_remaining_info() if passes >= MAX_PASS or 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