Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions spacecases/spacecases/commands/cs/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ async def open(
raise UserNotRegisteredError(interaction.user)

# dont have enough
if not rows[0]["deducted"]:
raise InsufficientBalanceError
balance_deducted: bool = rows[0]["deducted"]
if not balance_deducted:
balance_before_transaction: int = rows[0]["balance_before_transaction"]
raise InsufficientBalanceError(balance_before_transaction, price)

# generate probability table (maybe move to asset generation?)
cumulative_probabilities = {}
Expand All @@ -207,7 +209,6 @@ async def open(
container_entry = random.choice(container.contains[rarity])

# generate the item

match container_entry:
case SkinContainerEntry():
# calculate its float value
Expand Down
2 changes: 1 addition & 1 deletion spacecases/spacecases/commands/user/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def transfer(
# check we have enough balance
balance = rows[0]["balance"]
if balance < cents:
raise InsufficientBalanceError
raise InsufficientBalanceError(balance, cents)

# give their balance
rows = await bot.db.fetch_from_file_with_connection(
Expand Down
3 changes: 2 additions & 1 deletion spacecases/spacecases/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class UserNotRegisteredError(discord.app_commands.AppCommandError):

@dataclass
class InsufficientBalanceError(discord.app_commands.AppCommandError):
pass
current_balance: int
required_balance: int


@dataclass
Expand Down
11 changes: 7 additions & 4 deletions spacecases/spacecases/sql/user/money/try_deduct_balance.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ WITH balance_checker AS (
-- Check if the user has enough balance
SELECT
id,
balance >= $2 AS has_enough
balance >= $2 AS has_enough,
balance AS balance_before_transaction
FROM
"users"
WHERE
Expand All @@ -21,6 +22,8 @@ updated_user AS (
"users".id = $1
AND balance_checker.has_enough
)
SELECT has_enough as deducted
FROM balance_checker;

SELECT
balance_checker.has_enough AS deducted,
balance_checker.balance_before_transaction
FROM
balance_checker;
13 changes: 8 additions & 5 deletions spacecases/spacecases/ui/embed/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
UserInventoryEmptyError,
)
from common import get_logger
from spacecases.strutils import currency_str_format

logger = get_logger(__name__)

Expand All @@ -18,16 +19,18 @@ async def send_exception_embed(
) -> None:
if isinstance(exception, UserNotRegisteredError):
if interaction.user.id == exception.user.id:
message = "You are **not** registered!!!!!"
message = "You are **not** registered!"
else:
message = f"{exception.user.display_name} is **not** registered"
await send_err_embed(interaction, message, ephemeral)
elif isinstance(exception, InsufficientBalanceError):
await send_err_embed(
interaction,
"You **do not** have sufficient balance for this action",
ephemeral,
diff = exception.required_balance - exception.current_balance
e = discord.Embed(
title="You're broke!",
description=f"You need **{currency_str_format(diff)}** more to perform this action",
color=discord.Color.red(),
)
await interaction.response.send_message(embed=e, ephemeral=ephemeral)
elif isinstance(exception, ItemDoesNotExistError):
await send_err_embed(
interaction, f"Item `{exception.item}` does **not** exist", ephemeral
Expand Down