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
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
This commit is contained in:
parent
c9f172c451
commit
421946e316
2 changed files with 24 additions and 19 deletions
37
shop/shop.py
37
shop/shop.py
|
@ -92,6 +92,7 @@ class Shop(commands.Cog):
|
||||||
log.info("Loaded default shop configuration")
|
log.info("Loaded default shop configuration")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"Failed to load default shop: {e}")
|
log.error(f"Failed to load default shop: {e}")
|
||||||
|
log.exception("Full traceback:")
|
||||||
|
|
||||||
self.ready = True
|
self.ready = True
|
||||||
|
|
||||||
|
@ -1526,6 +1527,7 @@ class Parser:
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
self.instance = instance
|
self.instance = instance
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
self.is_init = ctx is None # Flag to indicate if this is initialization
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def basic_checks(idx, row):
|
def basic_checks(idx, row):
|
||||||
|
@ -1554,14 +1556,21 @@ class Parser:
|
||||||
elif row["Type"].lower() == "role" and not row["Role"]:
|
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))
|
log.warning("Row {} was not added because the type is a role, but no role was set.".format(idx))
|
||||||
return False
|
return False
|
||||||
elif (
|
elif not self.is_init: # Only do role checks if not in initialization
|
||||||
row["Type"].lower() == "role" and discord.utils.get(self.ctx.message.guild.roles, name=row["Role"]) is None
|
if row["Type"].lower() == "role" and discord.utils.get(self.ctx.message.guild.roles, name=row["Role"]) is None:
|
||||||
):
|
log.warning(
|
||||||
log.warning(
|
"Row {} was not added because the {} role does not exist on the server.".format(idx, row["Role"])
|
||||||
"Row {} was not added because the {} role does not exist on the server.".format(idx, row["Role"])
|
)
|
||||||
)
|
return False
|
||||||
return False
|
elif row["Type"].lower() == "role":
|
||||||
elif row["Type"].lower() == "auto" and int(row["Qty"]) == 0:
|
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))
|
log.warning("Row {} was not added because auto items cannot have an infinite quantity.".format(idx))
|
||||||
return False
|
return False
|
||||||
elif row["Type"].lower() == "auto" and int(row["Qty"]) != len(messages):
|
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):
|
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))
|
log.warning("Row {} was not added because one of the messages exceeds 2000 characters.".format(idx))
|
||||||
return False
|
return False
|
||||||
elif row["Type"].lower() == "role":
|
return True
|
||||||
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
|
|
||||||
|
|
||||||
async def parse_text_entry(self, text):
|
async def parse_text_entry(self, text):
|
||||||
keys = ("Shop", "Item", "Type", "Qty", "Cost", "Info", "Role", "Messages")
|
keys = ("Shop", "Item", "Type", "Qty", "Cost", "Info", "Role", "Messages")
|
||||||
|
|
|
@ -10,6 +10,7 @@ class ShopView(View):
|
||||||
self.shops = shops
|
self.shops = shops
|
||||||
self.current_shop = None
|
self.current_shop = None
|
||||||
self.current_item = None
|
self.current_item = None
|
||||||
|
self.quantity = None # Add quantity attribute
|
||||||
self.setup_shop_select()
|
self.setup_shop_select()
|
||||||
|
|
||||||
def setup_shop_select(self):
|
def setup_shop_select(self):
|
||||||
|
@ -80,6 +81,11 @@ class ShopView(View):
|
||||||
embed = purchase_view.build_embed()
|
embed = purchase_view.build_embed()
|
||||||
|
|
||||||
await interaction.response.edit_message(embed=embed, view=purchase_view)
|
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):
|
class PurchaseView(View):
|
||||||
def __init__(self, ctx, shop: str, item: str, item_data: Dict[str, Any], timeout: int = 60):
|
def __init__(self, ctx, shop: str, item: str, item_data: Dict[str, Any], timeout: int = 60):
|
||||||
|
|
Loading…
Add table
Reference in a new issue