From 421946e316bdd4830bcddba83a5f66db76b8ef0b Mon Sep 17 00:00:00 2001 From: Valerie Date: Sun, 25 May 2025 23:18:04 -0400 Subject: [PATCH] Enhance Shop cog by adding logging for exceptions during default shop loading and introducing a flag in the Parser class to differentiate initialization state. Update role validation logic to only execute outside of initialization, improving error handling and user experience during item parsing and shop interactions. --- shop/shop.py | 37 ++++++++++++++++++------------------- shop/ui.py | 6 ++++++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/shop/shop.py b/shop/shop.py index f61c367..0811e4a 100644 --- a/shop/shop.py +++ b/shop/shop.py @@ -92,6 +92,7 @@ class Shop(commands.Cog): log.info("Loaded default shop configuration") except Exception as e: log.error(f"Failed to load default shop: {e}") + log.exception("Full traceback:") self.ready = True @@ -1526,6 +1527,7 @@ class Parser: self.ctx = ctx self.instance = instance self.msg = msg + self.is_init = ctx is None # Flag to indicate if this is initialization @staticmethod def basic_checks(idx, row): @@ -1554,14 +1556,21 @@ class Parser: elif row["Type"].lower() == "role" and not row["Role"]: log.warning("Row {} was not added because the type is a role, but no role was set.".format(idx)) return False - elif ( - row["Type"].lower() == "role" and discord.utils.get(self.ctx.message.guild.roles, name=row["Role"]) is None - ): - log.warning( - "Row {} was not added because the {} role does not exist on the server.".format(idx, row["Role"]) - ) - return False - elif row["Type"].lower() == "auto" and int(row["Qty"]) == 0: + elif not self.is_init: # Only do role checks if not in initialization + if row["Type"].lower() == "role" and discord.utils.get(self.ctx.message.guild.roles, name=row["Role"]) is None: + log.warning( + "Row {} was not added because the {} role does not exist on the server.".format(idx, row["Role"]) + ) + return False + elif row["Type"].lower() == "role": + if discord.utils.get(self.ctx.message.guild.roles, name=row["Role"]) > self.ctx.author.top_role: + log.warning( + "Row {} was not added because the {} role is higher than the " + "shopkeeper's highest role.".format(idx, row["Role"]) + ) + return False + + if row["Type"].lower() == "auto" and int(row["Qty"]) == 0: log.warning("Row {} was not added because auto items cannot have an infinite quantity.".format(idx)) return False elif row["Type"].lower() == "auto" and int(row["Qty"]) != len(messages): @@ -1573,17 +1582,7 @@ class Parser: elif row["Type"].lower() == "auto" and any(len(x) > 2000 for x in messages): log.warning("Row {} was not added because one of the messages exceeds 2000 characters.".format(idx)) return False - elif row["Type"].lower() == "role": - if discord.utils.get(self.ctx.message.guild.roles, name=row["Role"]) > self.ctx.author.top_role: - log.warning( - "Row {} was not added because the {} role is higher than the " - "shopkeeper's highest role.".format(idx, row["Role"]) - ) - return False - else: - return True - else: - return True + return True async def parse_text_entry(self, text): keys = ("Shop", "Item", "Type", "Qty", "Cost", "Info", "Role", "Messages") diff --git a/shop/ui.py b/shop/ui.py index 6f4d472..a6ceba4 100644 --- a/shop/ui.py +++ b/shop/ui.py @@ -10,6 +10,7 @@ class ShopView(View): self.shops = shops self.current_shop = None self.current_item = None + self.quantity = None # Add quantity attribute self.setup_shop_select() def setup_shop_select(self): @@ -80,6 +81,11 @@ class ShopView(View): embed = purchase_view.build_embed() await interaction.response.edit_message(embed=embed, view=purchase_view) + + # Wait for the purchase view to complete + await purchase_view.wait() + self.quantity = purchase_view.quantity # Store the quantity from purchase view + self.stop() # Stop the shop view since we're done class PurchaseView(View): def __init__(self, ctx, shop: str, item: str, item_data: Dict[str, Any], timeout: int = 60):