Skip to content

Commit 7462c0b

Browse files
committed
fix conversion issue
1 parent 696fb01 commit 7462c0b

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

source/main.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2220,8 +2220,26 @@ async function handleBankConvert(interaction) {
22202220

22212221
// Only allow conversions that result in whole numbers
22222222
if (!Number.isInteger(finalConvertedAmount)) {
2223+
// Calculate nearest amounts that would result in whole number conversions
2224+
const lowerConvertedAmount = Math.floor(finalConvertedAmount);
2225+
const higherConvertedAmount = Math.ceil(finalConvertedAmount);
2226+
2227+
// Calculate the source amounts needed for these whole number conversions
2228+
const lowerSourceAmount = Math.round(lowerConvertedAmount * CURRENCY_TO_GOLD_CONVERSION[toCurrency] / CURRENCY_TO_GOLD_CONVERSION[fromCurrency]);
2229+
const higherSourceAmount = Math.round(higherConvertedAmount * CURRENCY_TO_GOLD_CONVERSION[toCurrency] / CURRENCY_TO_GOLD_CONVERSION[fromCurrency]);
2230+
2231+
let suggestions = [];
2232+
if (lowerConvertedAmount > 0 && lowerSourceAmount !== amount) {
2233+
suggestions.push(`\`${lowerSourceAmount}\` ${CURRENCY_ABBREVIATIONS[fromCurrency]} → \`${lowerConvertedAmount}\` ${CURRENCY_ABBREVIATIONS[toCurrency]}`);
2234+
}
2235+
if (higherSourceAmount !== amount) {
2236+
suggestions.push(`\`${higherSourceAmount}\` ${CURRENCY_ABBREVIATIONS[fromCurrency]} → \`${higherConvertedAmount}\` ${CURRENCY_ABBREVIATIONS[toCurrency]}`);
2237+
}
2238+
2239+
const suggestionText = suggestions.length > 0 ? `\n\nTry these amounts instead:\n${suggestions.join('\n')}` : '';
2240+
22232241
await interaction.editReply(
2224-
`Cannot convert \`${amount}\` ${CURRENCY_ABBREVIATIONS[fromCurrency]} to **${toCurrency}** - conversion must result in whole numbers. Try a different amount.`
2242+
`Cannot convert \`${amount}\` ${CURRENCY_ABBREVIATIONS[fromCurrency]} to **${toCurrency}** - conversion must result in whole numbers.${suggestionText}`
22252243
);
22262244
return;
22272245
}

0 commit comments

Comments
 (0)