Refactor inventory management in Shop cog to streamline item removal process. Implement user-specific checks for item quantity adjustments and ensure proper deletion of items when quantity reaches zero, enhancing inventory handling and user experience.
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
This commit is contained in:
parent
3482abef74
commit
2f0baffab2
1 changed files with 15 additions and 5 deletions
20
shop/shop.py
20
shop/shop.py
|
@ -1062,17 +1062,17 @@ class Shop(commands.Cog):
|
|||
async def redeem(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
if interaction.user != ctx.author:
|
||||
return await interaction.response.send_message("This menu is not for you!", ephemeral=True)
|
||||
await interaction.response.defer()
|
||||
self.value = True
|
||||
self.stop()
|
||||
await interaction.response.defer()
|
||||
|
||||
@discord.ui.button(label="Cancel", style=discord.ButtonStyle.red)
|
||||
async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
if interaction.user != ctx.author:
|
||||
return await interaction.response.send_message("This menu is not for you!", ephemeral=True)
|
||||
await interaction.response.defer()
|
||||
self.value = False
|
||||
self.stop()
|
||||
await interaction.response.defer()
|
||||
|
||||
if data[item]["Type"].lower() == "role":
|
||||
prompt = f"{ctx.author.mention} Do you wish to redeem {item}? This will grant you the role assigned to this item and it will be removed from your inventory permanently."
|
||||
|
@ -1095,9 +1095,11 @@ class Shop(commands.Cog):
|
|||
await self.assign_role(ctx, instance, item, data[item]["Role"])
|
||||
else:
|
||||
await self.pending_add(ctx, item)
|
||||
|
||||
sm = ShopManager(ctx, instance=None, user_data=instance)
|
||||
await sm.remove(item)
|
||||
async with instance.Inventory() as inv:
|
||||
if item in inv:
|
||||
inv[item]["Qty"] -= 1
|
||||
if inv[item]["Qty"] <= 0:
|
||||
del inv[item]
|
||||
|
||||
except Exception as exc:
|
||||
await msg.edit(content=f"An error occurred: {str(exc)}", view=None)
|
||||
|
@ -1413,6 +1415,14 @@ class ShopManager:
|
|||
inv[item] = deepcopy(item_data)
|
||||
inv[item]["Qty"] = quantity
|
||||
|
||||
async def remove(self, item, number=1):
|
||||
"""Remove an item from user's inventory."""
|
||||
async with self.user_data.Inventory() as inv:
|
||||
if item in inv:
|
||||
inv[item]["Qty"] -= number
|
||||
if inv[item]["Qty"] <= 0:
|
||||
del inv[item]
|
||||
|
||||
|
||||
class ItemManager:
|
||||
def __init__(self, ctx, instance):
|
||||
|
|
Loading…
Add table
Reference in a new issue