Enhance ShopManager and PurchaseView to improve user feedback with interactive embeds for error messages and purchase confirmations. Implement validation for item quantities and stock availability, ensuring clearer communication of issues and successful transactions.
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run

This commit is contained in:
Valerie 2025-05-25 23:42:19 -04:00
parent 5cc572ca7a
commit cc86a2f697
2 changed files with 82 additions and 11 deletions

View file

@ -1187,27 +1187,73 @@ class ShopManager:
try:
async with self.instance.Shops() as shops:
if shop not in shops:
return await self.ctx.send("That shop does not exist.")
return await self.ctx.send(
embed=discord.Embed(
title="Shop Not Found",
description="That shop does not exist.",
color=discord.Color.red()
)
)
if item not in shops[shop]["Items"]:
return await self.ctx.send("That item does not exist in the shop.")
return await self.ctx.send(
embed=discord.Embed(
title="Item Not Found",
description="That item does not exist in the shop.",
color=discord.Color.red()
)
)
item_data = deepcopy(shops[shop]["Items"][item])
except KeyError:
return await self.ctx.send("Could not locate that shop or item.")
return await self.ctx.send(
embed=discord.Embed(
title="Error",
description="Could not locate that shop or item.",
color=discord.Color.red()
)
)
# Validate quantity
if quantity is None:
return await self.ctx.send(
embed=discord.Embed(
title="Invalid Quantity",
description="No quantity specified for purchase.",
color=discord.Color.red()
)
)
cur = await bank.get_currency_name(self.ctx.guild)
stock, cost, _type = item_data["Qty"], item_data["Cost"], item_data["Type"]
# Check if item is in stock
if stock != "--" and stock <= 0:
return await self.ctx.send(f"Sorry, {item} is out of stock.")
return await self.ctx.send(
embed=discord.Embed(
title="Out of Stock",
description=f"Sorry, {item} is out of stock.",
color=discord.Color.red()
)
)
# Validate quantity for random items
if _type == "random" and quantity != 1:
return await self.ctx.send("You can only buy 1 random item at a time.")
return await self.ctx.send(
embed=discord.Embed(
title="Invalid Quantity",
description="You can only buy 1 random item at a time.",
color=discord.Color.red()
)
)
# Check if enough stock
if stock != "--" and quantity > stock:
return await self.ctx.send(f"Not enough stock! Only {stock} available.")
return await self.ctx.send(
embed=discord.Embed(
title="Insufficient Stock",
description=f"Not enough stock! Only {stock} available.",
color=discord.Color.red()
)
)
total_cost = cost * quantity
@ -1215,14 +1261,24 @@ class ShopManager:
await bank.withdraw_credits(self.ctx.author, total_cost)
except ValueError:
return await self.ctx.send(
f"You cannot afford {quantity}x {item} for {total_cost} {cur}. Transaction ended."
embed=discord.Embed(
title="Insufficient Funds",
description=f"You cannot afford {quantity}x {item} for {total_cost} {cur}.",
color=discord.Color.red()
)
)
# Handle different item types
if _type == "auto":
await self.auto_handler(shop, item, quantity)
await self.remove_stock(shop, item, stock, quantity)
return await self.ctx.send("Message sent.")
return await self.ctx.send(
embed=discord.Embed(
title="Auto Item Delivered",
description="Message sent.",
color=discord.Color.green()
)
)
if _type == "random":
new_item = await self.random_item(shop)
@ -1232,8 +1288,11 @@ class ShopManager:
except BalanceTooHigh as e:
await bank.set_balance(self.ctx.author, e.max_balance)
return await self.ctx.send(
f"There aren't any non-random items available in {shop}, "
f"so {item} cannot be purchased."
embed=discord.Embed(
title="No Items Available",
description=f"There aren't any non-random items available in {shop}, so {item} cannot be purchased.",
color=discord.Color.red()
)
)
else:
await self.remove_stock(shop, item, stock, quantity)
@ -1247,7 +1306,11 @@ class ShopManager:
await self.add_to_inventory(item, item_data, quantity)
await self.ctx.send(
f"{self.ctx.author.mention} purchased {quantity}x {item} for {total_cost} {cur}."
embed=discord.Embed(
title="Purchase Successful",
description=f"{self.ctx.author.mention} purchased {quantity}x {item} for {total_cost} {cur}.",
color=discord.Color.green()
)
)
async def remove_stock(self, shop, item, current_stock, amount):

View file

@ -173,6 +173,14 @@ class PurchaseView(View):
async def handle_purchase(self, interaction: discord.Interaction):
# Validate quantity
if self.quantity is None:
await interaction.response.send_message(
"Invalid quantity specified.",
ephemeral=True
)
return
# Validate quantity for random items
if self.item_data["Type"] == "random" and self.quantity != 1:
await interaction.response.send_message(
"You can only buy 1 random item at a time.",