3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-25 13:29:29 +01:00
pbot/applets/dice_roll.pl
Pragmatic Software 3d97dc2c33 Rename "modules" to "applets"
"Applet" is a much better name for the external command-line
scripts and programs that can be loaded as PBot commands. They
will no longer be confused with Perl modules.

https://en.wikipedia.org/wiki/Applet
2021-11-19 18:05:50 -08:00

40 lines
970 B
Perl
Executable File
Vendored

#!/usr/bin/env perl
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
# quick and dirty by :pragma
use Games::Dice qw/roll roll_array/;
my ($result, $rolls, $show);
if ($#ARGV < 0) {
print "Usage: roll [-show] <dice roll>; e.g.: roll 3d6+1. To see all individual dice rolls, add -show.\n";
die;
}
$rolls = join("", @ARGV);
if ($rolls =~ s/\s*-show\s*//) { $show = 1; }
if ($rolls =~ m/^\s*(\d+)d\d+(?:\+?-?\d+)?\s*$/) {
if ($1 > 100) {
print "Sorry, maximum of 100 rolls.\n";
die;
}
} else {
print "Usage: roll [-show] <dice roll>; e.g.: roll 3d6+1. To see all individual dice rolls, add -show.\n";
die;
}
if ($show) {
my @results = roll_array $rolls;
$result = 0;
foreach my $n (@results) { $result += $n; }
print "/me rolled $rolls for @results totaling $result.\n";
} else {
$result = roll $rolls;
print "/me rolled $rolls for $result.\n";
}