revbank/plugins/take

70 lines
1.7 KiB
Perl
Executable file

#!perl
HELP "take [<accounts> [<amount>]]" => "Take money from users (equal parts)";
sub command :Tab(take,steal) {
my ($self, $cart, $command) = @_;
$command eq 'take' or $command eq 'steal'
or return NEXT;
$self->{users} = [];
return "User to take from", \&arg;
}
sub arg :Tab(USERS) {
my ($self, $cart, $arg) = @_;
my @users = @{ $self->{users} };
my $amount = parse_amount($arg);
if (@users and $amount) {
my $each = sprintf "%.2f", $amount / @users;
my $total = sprintf "%.2f", @users * $each;
if ($total != $amount) {
print "Adjusted total amount to $total because of rounding.\n";
}
$self->{each} = $each;
$self->{total} = $total;
my $them = @users == 1 ? $users[0] : 'each';
return "Why are you taking $each from $them?", \&reason;
}
my $user = parse_user($arg);
if ($user) {
push @{ $self->{users} }, $user;
} else {
return REJECT, "$arg: No such user" .
($amount ? "." : ", and not a valid amount.");
}
return "User to take from, or total amount to finish", \&arg;
}
sub reason :Tab(bbq,NOABORT) { # finish
my ($self, $cart, $reason) = @_;
return REJECT, "'$reason' is a username, not a description :)."
if parse_user($reason);
return REJECT, "'$reason' is an amount, not a description :)."
if parse_amount($reason);
my @users = @{ $self->{users} };
my $each = $self->{each};
my $total = $self->{total};
for my $user (@users) {
$cart->add( $user, -$each, "Taken by \$you ($reason)" );
}
my $users = join '/', @users;
$cart->add( undef, $total, "Taken from $users ($reason)" );
return ACCEPT;
}