Skip to content

Commit f5bfcae

Browse files
authored
Make htd failure show the base trying to convert from/to (#964)
This makes the htd errors more clear to the user of what went wrong.
1 parent 1d58096 commit f5bfcae

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ Changes since 2026.06.04
112112
- Reworks the way the help command displays usage info
113113
- Mentions applications commands in the output now
114114

115+
### HTD
116+
- Is more clear on what errors were caused and why
117+
- Adds modulo as a valid operation
118+
115119
### Search
116120
- Now uses application commands
117121
- Now uses the Tavily API, to replace the deprcating google CSE API

modules/utility/htd.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ def perform_op_on_list(equation_list: list) -> int:
5858
Args:
5959
equation_list (list): The equation in a list form
6060
61-
Raises:
62-
ValueError: If the operator is not valid, this is raised
63-
6461
Returns:
6562
int: The integer value of the computed equation
63+
64+
Raises:
65+
ValueError: If the operator is not valid, this is raised
6666
"""
6767

6868
running_value = equation_list[0]
@@ -83,6 +83,8 @@ def perform_op_on_list(equation_list: list) -> int:
8383
running_value = running_value * value
8484
elif current_operator == "/":
8585
running_value = int(running_value / value)
86+
elif current_operator == "%":
87+
running_value = running_value % value
8688
else:
8789
raise ValueError("Invalid Equation")
8890
return running_value
@@ -114,11 +116,27 @@ def convert_list_to_ints(raw_list: list) -> list:
114116
115117
Returns:
116118
list: The same list you passed in, but with only ints
119+
120+
Raises:
121+
ValueError: If the conversion to an integer has failed, this will be raised
117122
"""
118123
for index, value in enumerate(raw_list):
119124
if index % 2 == 1:
120125
continue
121-
raw_list[index] = convert_value_to_integer(value)
126+
try:
127+
# Attempt to convert each value
128+
raw_list[index] = convert_value_to_integer(value)
129+
except ValueError as exc:
130+
# If conversion fails, get the base from the value
131+
if value.startswith("0x"):
132+
base = "hexadecimal"
133+
elif value.startswith("0b"):
134+
base = "binary"
135+
else:
136+
base = "decimal"
137+
138+
raise ValueError(f"Failed to convert `{value}` from {base} base.") from exc
139+
122140
return raw_list
123141

124142

@@ -250,7 +268,7 @@ def split_nicely(str_to_split: str) -> list:
250268
list: A list containing strings of the operators and numbers
251269
"""
252270

253-
OPERATORS = ["+", "-", "*", "/"]
271+
OPERATORS = ["+", "-", "*", "/", "%"]
254272

255273
parsed_list: list = []
256274
val_buffer = ""
@@ -311,11 +329,17 @@ async def htd(self: Self, ctx: commands.Context, *, val_to_convert: str) -> None
311329
# Convert the list to all ints
312330
try:
313331
int_list = convert_list_to_ints(parsed_list.copy())
314-
except ValueError:
315-
await auxiliary.send_deny_embed(
316-
message="Unable to convert value, are you sure it's valid?",
317-
channel=ctx.channel,
332+
except ValueError as e:
333+
# Handle the exception properly and send the embed here
334+
embed = auxiliary.prepare_deny_embed(
335+
message="Unable to convert value, are you sure it's valid?"
336+
)
337+
embed.add_field(
338+
name="Error Details",
339+
value=str(e),
340+
inline=False,
318341
)
342+
await ctx.send(embed=embed)
319343
return
320344

321345
# Attempt to parse the given equation and return a single integer answer

0 commit comments

Comments
 (0)