From 575e55cb0f6b3f32cbd69a7eec8188b7bdc7c2a7 Mon Sep 17 00:00:00 2001 From: Draper <27962761+Drapersniper@users.noreply.github.com> Date: Sun, 22 Sep 2019 07:05:27 +0100 Subject: [PATCH] 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 * 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> --- changelog.d/2997.misc.rst | 1 + redbot/core/bank.py | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 changelog.d/2997.misc.rst diff --git a/changelog.d/2997.misc.rst b/changelog.d/2997.misc.rst new file mode 100644 index 000000000..f2c131261 --- /dev/null +++ b/changelog.d/2997.misc.rst @@ -0,0 +1 @@ +Fixed a crash seen when calling economy commands in DM with a global bank. \ No newline at end of file diff --git a/redbot/core/bank.py b/redbot/core/bank.py index a0b2d9399..a51e5dc50 100644 --- a/redbot/core/bank.py +++ b/redbot/core/bank.py @@ -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 )