mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[Bank] Check recipient balance before completing transfer (#2925)
* Fixed `[p]local start` Playlist picker auto selects if theres just 1 playlist found `[p]queue cleanself` added Signed-off-by: Guy <guyreis96@gmail.com> * Black.... you are supposed to trigger before commits Signed-off-by: Guy <guyreis96@gmail.com> * Added `BalanceTooHigh` to the docstrings of `bank.transfer_credits()` Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com> * Bring this in line with #2926 to reduce conflicts,`is_global()` already is called inside `get_currency_name` and as such it does not need to be called outside Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>
This commit is contained in:
parent
68018c924e
commit
2056f9f8d0
1
changelog.d/2923.bugfix.rst
Normal file
1
changelog.d/2923.bugfix.rst
Normal file
@ -0,0 +1 @@
|
||||
Check the recipient balance before transferring and stop transfer if will go above the maximum allowed balance.
|
||||
@ -186,11 +186,7 @@ async def set_balance(member: discord.Member, amount: int) -> int:
|
||||
if amount < 0:
|
||||
raise ValueError("Not allowed to have negative balance.")
|
||||
if amount > MAX_BALANCE:
|
||||
currency = (
|
||||
await get_currency_name()
|
||||
if await is_global()
|
||||
else await get_currency_name(member.guild)
|
||||
)
|
||||
currency = await get_currency_name(member.guild)
|
||||
raise errors.BalanceTooHigh(
|
||||
user=member.display_name, max_balance=MAX_BALANCE, currency_name=currency
|
||||
)
|
||||
@ -305,6 +301,9 @@ async def transfer_credits(from_: discord.Member, to: discord.Member, amount: in
|
||||
If the amount is invalid or if ``from_`` has insufficient funds.
|
||||
TypeError
|
||||
If the amount is not an `int`.
|
||||
BalanceTooHigh
|
||||
If the balance after the transfer would be greater than
|
||||
``bank.MAX_BALANCE``
|
||||
|
||||
"""
|
||||
if not isinstance(amount, int):
|
||||
@ -312,6 +311,12 @@ async def transfer_credits(from_: discord.Member, to: discord.Member, amount: in
|
||||
if _invalid_amount(amount):
|
||||
raise ValueError("Invalid transfer amount {} <= 0".format(amount))
|
||||
|
||||
if await get_balance(to) + amount > MAX_BALANCE:
|
||||
currency = await get_currency_name(to.guild)
|
||||
raise errors.BalanceTooHigh(
|
||||
user=to.display_name, max_balance=MAX_BALANCE, currency_name=currency
|
||||
)
|
||||
|
||||
await withdraw_credits(from_, amount)
|
||||
return await deposit_credits(to, amount)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user