[Economy] Improved [p]bank set

Can now accept positive/negative values to add and remove credits
This commit is contained in:
Twentysix 2016-12-18 02:17:35 +01:00
parent 74fd56e81f
commit ae6756c637

View File

@ -199,6 +199,28 @@ class Bank:
raise NoAccount() raise NoAccount()
class SetParser:
def __init__(self, argument):
allowed = ("+", "-")
if argument and argument[0] in allowed:
try:
self.sum = int(argument)
except:
raise
if self.sum < 0:
self.operation = "withdraw"
elif self.sum > 0:
self.operation = "deposit"
else:
raise
self.sum = abs(self.sum)
elif argument.isdigit():
self.sum = int(argument)
self.operation = "set"
else:
raise
class Economy: class Economy:
"""Economy """Economy
@ -281,17 +303,38 @@ class Economy:
@_bank.command(name="set", pass_context=True) @_bank.command(name="set", pass_context=True)
@checks.admin_or_permissions(manage_server=True) @checks.admin_or_permissions(manage_server=True)
async def _set(self, ctx, user: discord.Member, sum: int): async def _set(self, ctx, user: discord.Member, credits: SetParser):
"""Sets credits of user's bank account """Sets credits of user's bank account. See help for more operations
Admin/owner restricted.""" Passing positive and negative values will add/remove credits instead
Examples:
bank set @Twentysix 26 - Sets 26 credits
bank set @Twentysix +2 - Adds 2 credits
bank set @Twentysix -6 - Removes 6 credits"""
author = ctx.message.author author = ctx.message.author
try: try:
self.bank.set_credits(user, sum) if credits.operation == "deposit":
logger.info("{}({}) set {} credits to {} ({})".format( self.bank.deposit_credits(user, credits.sum)
author.name, author.id, str(sum), user.name, user.id)) logger.info("{}({}) added {} credits to {} ({})".format(
await self.bot.say("{}'s credits have been set to {}".format( author.name, author.id, credits.sum, user.name, user.id))
user.name, str(sum))) await self.bot.say("{} credits have been added to {}"
"".format(credits.sum, user.name))
elif credits.operation == "withdraw":
self.bank.withdraw_credits(user, credits.sum)
logger.info("{}({}) removed {} credits to {} ({})".format(
author.name, author.id, credits.sum, user.name, user.id))
await self.bot.say("{} credits have been withdrawn from {}"
"".format(credits.sum, user.name))
elif credits.operation == "set":
self.bank.set_credits(user, credits.sum)
logger.info("{}({}) set {} credits to {} ({})"
"".format(author.name, author.id, credits.sum,
user.name, user.id))
await self.bot.say("{}'s credits have been set to {}".format(
user.name, credits.sum))
except InsufficientBalance:
await self.bot.say("User doesn't have enough credits.")
except NoAccount: except NoAccount:
await self.bot.say("User has no bank account.") await self.bot.say("User has no bank account.")