Fixed crash originated in bank.set_balance (#2997)

* Removes `MAX_BALANCE` from bank, user `bank.get_max_balance()` now
`[p]bankset maxbal` can be used to set the maximum bank balance

Signed-off-by: Guy <guyreis96@gmail.com>

* Pushed the Fix for new issue introduced by #2926

Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>

* Changelogs

Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>

* Rename changelog to a misc

Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>

* Address Flame's review

Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>

* Correct an outdated reference in the docs

Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>

* Reword docstring for the RuntimeError

Signed-off-by: guyre <27962761+drapersniper@users.noreply.github.com>
This commit is contained in:
Draper 2019-09-22 07:05:27 +01:00 committed by Michael H
parent ee3be8b633
commit 575e55cb0f
2 changed files with 22 additions and 13 deletions

View File

@ -0,0 +1 @@
Fixed a crash seen when calling economy commands in DM with a global bank.

View File

@ -170,12 +170,12 @@ async def can_spend(member: discord.Member, amount: int) -> bool:
return await get_balance(member) >= amount
async def set_balance(member: discord.Member, amount: int) -> int:
async def set_balance(member: Union[discord.Member, discord.User], amount: int) -> int:
"""Set an account balance.
Parameters
----------
member : discord.Member
member : Union[discord.Member, discord.User]
The member whose balance to set.
amount : int
The amount to set the balance to.
@ -189,16 +189,19 @@ async def set_balance(member: discord.Member, amount: int) -> int:
------
ValueError
If attempting to set the balance to a negative number.
RuntimeError
If the bank is guild-specific and a discord.User object is provided.
BalanceTooHigh
If attempting to set the balance to a value greater than
``bank.MAX_BALANCE``
``bank._MAX_BALANCE``.
"""
if amount < 0:
raise ValueError("Not allowed to have negative balance.")
max_bal = await get_max_balance(member.guild)
guild = getattr(member, "guild", None)
max_bal = await get_max_balance(guild)
if amount > max_bal:
currency = await get_currency_name(member.guild)
currency = await get_currency_name(guild)
raise errors.BalanceTooHigh(
user=member.display_name, max_balance=max_bal, currency_name=currency
)
@ -303,14 +306,18 @@ async def deposit_credits(member: discord.Member, amount: int) -> int:
return await set_balance(member, amount + bal)
async def transfer_credits(from_: discord.Member, to: discord.Member, amount: int):
async def transfer_credits(
from_: Union[discord.Member, discord.User],
to: Union[discord.Member, discord.User],
amount: int,
):
"""Transfer a given amount of credits from one account to another.
Parameters
----------
from_: discord.Member
from_: Union[discord.Member, discord.User]
The member to transfer from.
to : discord.Member
to : Union[discord.Member, discord.User]
The member to transfer to.
amount : int
The amount to transfer.
@ -326,10 +333,11 @@ 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`.
RuntimeError
If the bank is guild-specific and a discord.User object is provided.
BalanceTooHigh
If the balance after the transfer would be greater than
``bank.MAX_BALANCE``
``bank._MAX_BALANCE``.
"""
if not isinstance(amount, int):
raise TypeError("Transfer amount must be of type int, not {}.".format(type(amount)))
@ -339,11 +347,11 @@ async def transfer_credits(from_: discord.Member, to: discord.Member, amount: in
humanize_number(amount, override_locale="en_US")
)
)
max_bal = await get_max_balance(to.guild)
guild = getattr(to, "guild", None)
max_bal = await get_max_balance(guild)
if await get_balance(to) + amount > max_bal:
currency = await get_currency_name(to.guild)
currency = await get_currency_name(guild)
raise errors.BalanceTooHigh(
user=to.display_name, max_balance=max_bal, currency_name=currency
)