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

This commit is contained in:
Valerie 2025-05-25 23:18:04 -04:00
parent c9f172c451
commit 421946e316
2 changed files with 24 additions and 19 deletions

View file

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

View file

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