From 359b52888e5a817caad11f5cbddfb18790d2030b Mon Sep 17 00:00:00 2001 From: praveenscript Date: Mon, 6 Apr 2026 13:53:24 +0530 Subject: [PATCH] fix: prevent trivia questions repetition by using shuffled sequence --- cogs/fun.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cogs/fun.py b/cogs/fun.py index d912082..afa003a 100644 --- a/cogs/fun.py +++ b/cogs/fun.py @@ -81,6 +81,7 @@ class Fun(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot + self.question_index = 0 self._absolute_template_cache_bytes: Optional[bytes] = None self._absolute_template_cache_expires_at = 0.0 self._absolute_template_cache_lock = asyncio.Lock() @@ -165,6 +166,17 @@ def _build_absolute_gif(cls, template_bytes: bytes, avatar_bytes: bytes, text: s result.seek(0) return result + def get_next_question(self) -> dict[str, str]: + + if self.question_index >= len(TRIVIA_QUESTIONS): + random.shuffle(TRIVIA_QUESTIONS) + self.question_index = 0 + + question = TRIVIA_QUESTIONS[self.question_index] + self.question_index += 1 + + return question + async def _get_absolute_template_bytes(self) -> bytes: now = time.monotonic() if self._absolute_template_cache_bytes and now < self._absolute_template_cache_expires_at: @@ -268,7 +280,7 @@ async def fortune(self, ctx: commands.Context): @commands.hybrid_command(name="trivia", help="Answer a programming trivia question") async def trivia(self, ctx: commands.Context): """Start a programming trivia question.""" - question_data = random.choice(TRIVIA_QUESTIONS) + question_data = self.get_next_question() embed = discord.Embed( title="Programming Trivia",