diff options
Diffstat (limited to 'geldschieberbot.py')
| -rw-r--r-- | geldschieberbot.py | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/geldschieberbot.py b/geldschieberbot.py index 1ae85ed..4488114 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -227,10 +227,10 @@ class Geldschieberbot: split amount person [persons] - split amount between the sender and persons teil amount person [persons] - split amount between the sender and persons - schieb amount recipient - give money to recipient - gib amount recipient - give money to recipient - zieh amount donor - get money from donor - nimm amount donor - get money from donor + schieb amount recipient [recipients ...] - give money to one or more recipients + gib amount recipient [recipients ...] - give money to one or more recipients + zieh amount donor [donor ...] - get money from one or more donors + nimm amount donor [donor ...] - get money from one or more donors transfer amount source destination - transfer amount of your balance from source to destination @@ -365,22 +365,30 @@ class Geldschieberbot: def transaction(self, sender, args, msg) -> dict[str, str]: # pylint: disable=unused-argument """Record a transaction""" - if len(args) != 3: - return {'err': f'not in form "{args[0]} amount recipient"'} + if len(args) < 3: + return { + 'err': + f'not in form "{args[0]} amount recipient [recipient ...]"' + } if not sender in self.balance: if sender not in self.num2name: return {'err': 'you must register first'} sender = self.num2name[sender] - if args[1] in self.balance: - recipient, amount = args[1:3] - elif args[2] in self.balance: - amount, recipient = args[1:3] + if len(args) == 3: + if args[1] in self.balance or args[1] in self.aliases: + recipient, amount = args[1:3] + elif args[2] in self.balance or args[2] in self.aliases: + amount, recipient = args[1:3] + else: + return {'err': 'recipient not known'} + + recipients = self.expand_aliases([recipient]) else: - return {'err': 'recipient not known'} + amount, recipients = args[1], args[2:] - if sender == recipient: + if sender in recipients: return {'err': 'you can not transfer money to or from yourself'} try: @@ -388,19 +396,34 @@ class Geldschieberbot: except (ValueError, TypeError): return {'err': 'amount must be a positive number'} + for recipient in recipients: + if recipient not in self.balance: + return {'err': f'recipient "{recipient}" not known'} + if args[0] in ["!zieh", "!nimm"]: amount *= -1 - self.may_record_change(sender, [args, [sender, recipient, amount]]) + transaction_sum = '' - self.record(sender, recipient, amount) + change = [args] + for recipient in recipients: + change.append([sender, recipient, amount]) + self.record(sender, recipient, amount) - p_balance = self.balance[sender][recipient] + p_balance = self.balance[sender][recipient] - output = ("New Balance: {} {} {} {}\n".format( - sender, ("->" if p_balance > 0 else "<-"), to_euro(abs(p_balance)), - recipient)) - return {'msg': output} + transaction_sum += f'{"->" if amount > 0 else "<-"} {recipient} {to_euro(abs(amount))}\n' + + self.may_record_change(sender, change) + + output = '' + if len(recipients) > 1: + output += transaction_sum + output += 'New Balance:\n' + output += self.create_summary(sender, recipients) + else: + output = f'New Balance: {sender} {"->" if p_balance > 0 else "<-"} {to_euro(abs(p_balance))} {recipient}\n' + return {'msg': f'{output}'} def transfer(self, sender, args, msg) -> dict[str, str]: # pylint: disable=unused-argument """Transfer amount from one balance to another""" |
