diff --git a/changelog.md b/changelog.md index 9f1fbcfa..7a90870b 100644 --- a/changelog.md +++ b/changelog.md @@ -112,6 +112,10 @@ Changes since 2026.06.04 - Reworks the way the help command displays usage info - Mentions applications commands in the output now +### HTD +- Is more clear on what errors were caused and why +- Adds modulo as a valid operation + ### Search - Now uses application commands - Now uses the Tavily API, to replace the deprcating google CSE API diff --git a/modules/utility/htd.py b/modules/utility/htd.py index e54fad68..84738332 100644 --- a/modules/utility/htd.py +++ b/modules/utility/htd.py @@ -58,11 +58,11 @@ def perform_op_on_list(equation_list: list) -> int: Args: equation_list (list): The equation in a list form - Raises: - ValueError: If the operator is not valid, this is raised - Returns: int: The integer value of the computed equation + + Raises: + ValueError: If the operator is not valid, this is raised """ running_value = equation_list[0] @@ -83,6 +83,8 @@ def perform_op_on_list(equation_list: list) -> int: running_value = running_value * value elif current_operator == "/": running_value = int(running_value / value) + elif current_operator == "%": + running_value = running_value % value else: raise ValueError("Invalid Equation") return running_value @@ -114,11 +116,27 @@ def convert_list_to_ints(raw_list: list) -> list: Returns: list: The same list you passed in, but with only ints + + Raises: + ValueError: If the conversion to an integer has failed, this will be raised """ for index, value in enumerate(raw_list): if index % 2 == 1: continue - raw_list[index] = convert_value_to_integer(value) + try: + # Attempt to convert each value + raw_list[index] = convert_value_to_integer(value) + except ValueError as exc: + # If conversion fails, get the base from the value + if value.startswith("0x"): + base = "hexadecimal" + elif value.startswith("0b"): + base = "binary" + else: + base = "decimal" + + raise ValueError(f"Failed to convert `{value}` from {base} base.") from exc + return raw_list @@ -250,7 +268,7 @@ def split_nicely(str_to_split: str) -> list: list: A list containing strings of the operators and numbers """ - OPERATORS = ["+", "-", "*", "/"] + OPERATORS = ["+", "-", "*", "/", "%"] parsed_list: list = [] val_buffer = "" @@ -311,11 +329,17 @@ async def htd(self: Self, ctx: commands.Context, *, val_to_convert: str) -> None # Convert the list to all ints try: int_list = convert_list_to_ints(parsed_list.copy()) - except ValueError: - await auxiliary.send_deny_embed( - message="Unable to convert value, are you sure it's valid?", - channel=ctx.channel, + except ValueError as e: + # Handle the exception properly and send the embed here + embed = auxiliary.prepare_deny_embed( + message="Unable to convert value, are you sure it's valid?" + ) + embed.add_field( + name="Error Details", + value=str(e), + inline=False, ) + await ctx.send(embed=embed) return # Attempt to parse the given equation and return a single integer answer