From b609e7f7569ea4046c90afd932b3302444e997de Mon Sep 17 00:00:00 2001 From: Sayed Imtiazuddin Date: Tue, 3 Jun 2025 15:31:13 -0400 Subject: [PATCH 1/4] Initial commi for Sayed I. --- .../cogs/onboarding/sayed_onboarding_cog.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py diff --git a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py new file mode 100644 index 0000000..99389ff --- /dev/null +++ b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py @@ -0,0 +1,39 @@ +import discord +import logging +from discord.ext import commands +from discord import app_commands + +from frontend import config_colors as colors +from config import settings +import asyncio + + +class SayedCog(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + self.logger = logging.getLogger( + f"discord.cog.{self.__class__.__name__.lower()}" + ) + + @app_commands.guilds(discord.Object(id=settings.DEBUG_GUILD_ID)) + @app_commands.command(name="countdown", description="Counts down the input seconds to 0. Allowed range is 1-30 seconds.") + async def countdown(self, interaction: discord.Interaction, seconds: int): + if seconds < 1 or seconds > 30: + await interaction.response.send_message( + "Please provide a number between 1 and 30 seconds.", + ephemeral=True, + ) + return + await interaction.response.send_message( + f"Counting down from {seconds} seconds...", + ephemeral=True, + ) + + for i in range(seconds, 0, -1): + await asyncio.sleep(1) + await interaction.edit_original_response(content=f"{i}...") + await asyncio.sleep(1) + await interaction.edit_original_response(content="Time's up!") + +async def setup(bot: commands.Bot): + await bot.add_cog(SayedCog(bot)) From 679a3901e31ae06ca68f6b8f5b769864e6595573 Mon Sep 17 00:00:00 2001 From: Sayed Imtiazuddin Date: Tue, 3 Jun 2025 16:38:27 -0400 Subject: [PATCH 2/4] Make messages visible --- .../frontend/cogs/onboarding/sayed_onboarding_cog.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py index 99389ff..7820bb5 100644 --- a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py +++ b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py @@ -20,13 +20,11 @@ def __init__(self, bot: commands.Bot): async def countdown(self, interaction: discord.Interaction, seconds: int): if seconds < 1 or seconds > 30: await interaction.response.send_message( - "Please provide a number between 1 and 30 seconds.", - ephemeral=True, + "Please provide a number between 1 and 30 seconds." ) return await interaction.response.send_message( - f"Counting down from {seconds} seconds...", - ephemeral=True, + f"Counting down from {seconds} seconds..." ) for i in range(seconds, 0, -1): From 208a0be7b067df05a6683396193c01f228980d6b Mon Sep 17 00:00:00 2001 From: Sayed Imtiazuddin Date: Wed, 4 Jun 2025 19:13:16 -0400 Subject: [PATCH 3/4] Improved exception and error handling --- .../cogs/onboarding/sayed_onboarding_cog.py | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py index 7820bb5..a792d57 100644 --- a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py +++ b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py @@ -1,11 +1,12 @@ import discord import logging +import asyncio from discord.ext import commands from discord import app_commands from frontend import config_colors as colors from config import settings -import asyncio + class SayedCog(commands.Cog): @@ -17,21 +18,25 @@ def __init__(self, bot: commands.Bot): @app_commands.guilds(discord.Object(id=settings.DEBUG_GUILD_ID)) @app_commands.command(name="countdown", description="Counts down the input seconds to 0. Allowed range is 1-30 seconds.") - async def countdown(self, interaction: discord.Interaction, seconds: int): - if seconds < 1 or seconds > 30: - await interaction.response.send_message( - "Please provide a number between 1 and 30 seconds." - ) - return - await interaction.response.send_message( - f"Counting down from {seconds} seconds..." - ) - + async def countdown(self, interaction: discord.Interaction, seconds: app_commands.Range[int, 1, 30]): + # Defer the response to avoid a timeout + await interaction.response.defer() + + try: + await interaction.edit_original_response(content=f"Counting down from {seconds} seconds...") + except discord.HTTPException as e: + self.logger.error(f"Failed to edit initial response: {e}") for i in range(seconds, 0, -1): await asyncio.sleep(1) - await interaction.edit_original_response(content=f"{i}...") + try: + await interaction.edit_original_response(content=f"{i}...") + except discord.HTTPException as e: + self.logger.error(f"Failed to edit countdown response at {i}: {e}") await asyncio.sleep(1) - await interaction.edit_original_response(content="Time's up!") + try: + await interaction.edit_original_response(content="Time's up!") + except discord.HTTPException as e: + self.logger.error(f"Failed to edit final response: {e}") async def setup(bot: commands.Bot): await bot.add_cog(SayedCog(bot)) From 04fe48f8828ba55724f17d616933d29e4af6a472 Mon Sep 17 00:00:00 2001 From: Sayed Imtiazuddin Date: Sun, 8 Jun 2025 23:29:34 -0400 Subject: [PATCH 4/4] added arithmetic_game --- .../cogs/onboarding/sayed_onboarding_cog.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py index a792d57..06abe1b 100644 --- a/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py +++ b/src/capy_app/frontend/cogs/onboarding/sayed_onboarding_cog.py @@ -6,6 +6,7 @@ from frontend import config_colors as colors from config import settings +import random @@ -37,6 +38,47 @@ async def countdown(self, interaction: discord.Interaction, seconds: app_command await interaction.edit_original_response(content="Time's up!") except discord.HTTPException as e: self.logger.error(f"Failed to edit final response: {e}") + + async def arithmetic_game(self, interaction: discord.Interaction): + # You can implement the game logic here + num1 = random.randint(1, 10) + num2 = random.randint(1, 10) + operation = random.choice(["+", "-", "*", "/"]) + + challenge = f"What is {num1} {operation} {num2}?" + await interaction.followup.send(challenge) + + if operation == '+': + correct_answer = num1 + num2 + elif operation == '-': + correct_answer = num1 - num2 + elif operation == '*': + correct_answer = num1 * num2 + elif operation == '/': + correct_answer = num1 / num2 + else: + raise ValueError(f"Unsupported operation: {operation}") + + def check(m): + return m.author == interaction.user and m.channel == interaction.channel + + try: + msg = await self.bot.wait_for('message', check=check, timeout=30.0) + except asyncio.TimeoutError: + await interaction.followup.send("Timed out! You took too long to answer.") + return + + try: + user_answer = float(msg.content) + except ValueError: + await interaction.followup.send("Invalid answer. Please enter a numeric value.") + return + + if abs(user_answer - correct_answer) < 0.001: + await interaction.followup.send("Correct!") + else: + await interaction.followup.send(f"Incorrect. The correct answer was {correct_answer}.") + async def setup(bot: commands.Bot): await bot.add_cog(SayedCog(bot))