Ensure Bank API only works with Integers. (#4376)

Co-authored-by: Draper <27962761+Drapersniper@users.noreply.github.com>
This commit is contained in:
Ian Martin 2020-08-31 09:24:44 -04:00 committed by GitHub
parent e1261b92d1
commit 7d24413846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -191,6 +191,11 @@ async def can_spend(member: discord.Member, amount: int) -> bool:
amount : int amount : int
The amount the member wants to spend. The amount the member wants to spend.
Raises
------
TypeError
If the amount is not an `int`.
Returns Returns
------- -------
bool bool
@ -198,6 +203,8 @@ async def can_spend(member: discord.Member, amount: int) -> bool:
amount, else :code:`False`. amount, else :code:`False`.
""" """
if not isinstance(amount, int):
raise TypeError("Amount must be of type int, not {}.".format(type(amount)))
if _invalid_amount(amount): if _invalid_amount(amount):
return False return False
return await get_balance(member) >= amount return await get_balance(member) >= amount
@ -212,7 +219,6 @@ async def set_balance(member: Union[discord.Member, discord.User], amount: int)
The member whose balance to set. The member whose balance to set.
amount : int amount : int
The amount to set the balance to. The amount to set the balance to.
Returns Returns
------- -------
int int
@ -227,8 +233,12 @@ async def set_balance(member: Union[discord.Member, discord.User], amount: int)
BalanceTooHigh BalanceTooHigh
If attempting to set the balance to a value greater than If attempting to set the balance to a value greater than
``bank._MAX_BALANCE``. ``bank._MAX_BALANCE``.
TypeError
If the amount is not an `int`.
""" """
if not isinstance(amount, int):
raise TypeError("Amount must be of type int, not {}.".format(type(amount)))
if amount < 0: if amount < 0:
raise ValueError("Not allowed to have negative balance.") raise ValueError("Not allowed to have negative balance.")
guild = getattr(member, "guild", None) guild = getattr(member, "guild", None)
@ -799,7 +809,12 @@ async def set_max_balance(amount: int, guild: discord.Guild = None) -> int:
If the bank is guild-specific and guild was not provided. If the bank is guild-specific and guild was not provided.
ValueError ValueError
If the amount is less than 0 or higher than 2 ** 63 - 1. If the amount is less than 0 or higher than 2 ** 63 - 1.
TypeError
If the amount is not an `int`.
""" """
if not isinstance(amount, int):
raise TypeError("Amount must be of type int, not {}.".format(type(amount)))
if not (0 < amount <= _MAX_BALANCE): if not (0 < amount <= _MAX_BALANCE):
raise ValueError( raise ValueError(
"Amount must be greater than zero and less than {max}.".format( "Amount must be greater than zero and less than {max}.".format(
@ -868,9 +883,12 @@ async def set_default_balance(amount: int, guild: discord.Guild = None) -> int:
If the bank is guild-specific and guild was not provided. If the bank is guild-specific and guild was not provided.
ValueError ValueError
If the amount is less than 0 or higher than the max allowed balance. If the amount is less than 0 or higher than the max allowed balance.
TypeError
If the amount is not an `int`.
""" """
amount = int(amount) if not isinstance(amount, int):
raise TypeError("Amount must be of type int, not {}.".format(type(amount)))
max_bal = await get_max_balance(guild) max_bal = await get_max_balance(guild)
if not (0 <= amount <= max_bal): if not (0 <= amount <= max_bal):
@ -905,7 +923,9 @@ def cost(amount: int):
Other exceptions will propagate and will be handled by Red's (and/or Other exceptions will propagate and will be handled by Red's (and/or
any other configured) error handling. any other configured) error handling.
""" """
# TODO: Add documentation for input/output/exceptions
if not isinstance(amount, int) or amount < 0: if not isinstance(amount, int) or amount < 0:
raise ValueError("This decorator requires an integer cost greater than or equal to zero") raise ValueError("This decorator requires an integer cost greater than or equal to zero")