From 9b7be4987f3c349517d13540069256a308aa1b60 Mon Sep 17 00:00:00 2001 From: Valerie Date: Sun, 25 May 2025 23:26:44 -0400 Subject: [PATCH] Refactor Shop cog to improve exception handling during shop loading and enhance user experience by implementing a state flag in the Parser class. Update role validation logic to ensure it only runs outside of initialization, streamlining item parsing and shop interactions. --- shop/data/role_shop.csv | 11 +++++++++++ shop/shop.py | 4 ++-- shop/ui.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 shop/data/role_shop.csv diff --git a/shop/data/role_shop.csv b/shop/data/role_shop.csv new file mode 100644 index 0000000..2def17c --- /dev/null +++ b/shop/data/role_shop.csv @@ -0,0 +1,11 @@ +Shop,Item,Type,Qty,Cost,Info,Role,Messages +Role Store,VIP Access,role,50,5000,Grants you the VIP role with special channel access.,VIP, +Role Store,Premium Member,role,25,10000,Become a premium member with exclusive perks!,Premium, +Role Store,Custom Color,role,100,2500,Get a fancy colored name with the Color role.,Color, +Role Store,DJ Role,role,30,3000,Access to music bot controls and DJ features.,DJ, +Role Store,Game Master,role,20,7500,Organize and manage game events on the server.,Game Master, +Role Store,Content Creator,role,15,5000,Special role for content creators with dedicated channels.,Content Creator, +Role Store,Event Host,role,25,4000,Host server events and get access to event planning channels.,Event Host, +Role Store,Trader,role,50,3500,Access to special trading channels and features.,Trader, +Role Store,Supporter,role,100,1500,Show your support for the server with this special role.,Supporter, +Role Store,Veteran,role,10,15000,An exclusive role for long-time dedicated members.,Veteran \ No newline at end of file diff --git a/shop/shop.py b/shop/shop.py index 0811e4a..589556d 100644 --- a/shop/shop.py +++ b/shop/shop.py @@ -128,8 +128,8 @@ class Shop(commands.Cog): try: await view.wait() - if view.value: # An item was selected to use - await self.pending_prompt(ctx, instance, inventory, view.value) + if view.selected_item: # An item was selected to use + await self.pending_prompt(ctx, instance, inventory, view.selected_item) except asyncio.TimeoutError: await ctx.send("Inventory view timed out.") diff --git a/shop/ui.py b/shop/ui.py index a6ceba4..602a390 100644 --- a/shop/ui.py +++ b/shop/ui.py @@ -185,6 +185,7 @@ class InventoryView(View): super().__init__(timeout=timeout) self.ctx = ctx self.inventory = inventory + self.selected_item = None # Track selected item self.setup_inventory_select() def setup_inventory_select(self): @@ -211,6 +212,7 @@ class InventoryView(View): return await interaction.response.send_message("This menu is not for you!", ephemeral=True) item_name = interaction.data["values"][0] + self.selected_item = item_name # Store selected item item_data = self.inventory[item_name] embed = discord.Embed( @@ -224,6 +226,11 @@ class InventoryView(View): if item_data["Type"] == "role": use_view = UseItemView(self.ctx, item_name, item_data) await interaction.response.edit_message(embed=embed, view=use_view) + + # Wait for the use view to complete + await use_view.wait() + if use_view.value: # If item was used + self.stop() # Stop the inventory view else: await interaction.response.edit_message(embed=embed) @@ -233,12 +240,14 @@ class UseItemView(View): self.ctx = ctx self.item = item self.item_data = item_data + self.value = False # Track if item was used @button(label="Use Item", style=discord.ButtonStyle.green) async def use_item(self, interaction: discord.Interaction, button: Button): if interaction.user != self.ctx.author: return await interaction.response.send_message("This menu is not for you!", ephemeral=True) + self.value = True # Item was used await interaction.response.edit_message( content=f"Processing use of {self.item}...", embed=None, @@ -250,5 +259,6 @@ class UseItemView(View): async def cancel(self, interaction: discord.Interaction, button: Button): if interaction.user != self.ctx.author: return await interaction.response.send_message("This menu is not for you!", ephemeral=True) + self.value = False # Item was not used await interaction.response.edit_message(content="Cancelled.", embed=None, view=None) self.stop() \ No newline at end of file