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.
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run

This commit is contained in:
Valerie 2025-05-25 23:26:44 -04:00
parent 421946e316
commit 9b7be4987f
3 changed files with 23 additions and 2 deletions

11
shop/data/role_shop.csv Normal file
View file

@ -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
Can't render this file because it has a wrong number of fields in line 11.

View file

@ -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.")

View file

@ -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()