use Net::IRC;
use Getopt::Long;
use Pod::Usage;
use strict;
my $VERSION = "1.1.0";
my $server = "";
my @room;
my $room;
my $port = 6667;
my $help = 0;
my $versions = 0;
my $pass = '';
my $name = "Roller";
GetOptions(
'help|h' => \$help,
'port|p=i' => \$port,
'versions|v' => \$versions,
'pass|p=s' => \$pass,
'name|n=s' => \$name,
) or (pod2usage() && die);
if($help){
pod2usage(-verbose=>1);
exit;
}
if($versions){
print
"\nModules, Perl, OS, Program info:\n",
" Net::IRC $Net::IRC::VERSION\n",
" Pod::Usage $Pod::Usage::VERSION\n",
" Getopt::Long $Getopt::Long::VERSION\n",
" strict $strict::VERSION\n",
" Perl $]\n",
" OS $^O\n",
" irc.pl $VERSION\n",
" $0\n\n";
exit;
}
{
local $@;
eval{
$server = shift(@ARGV) or (pod2usage() && die);
@room = @ARGV or (pod2usage() && die);
};
if($@ =~ m#Can't open -e for reading#){
#Used with tinyperl
pod2usage(-verbose=>1);
exit;
}elsif($@){
die($@);
}
}
my $irc = new Net::IRC;
my $conn = $irc->newconn(
Nick => $name,
Server => $server,
Port => $port,
Ircname => 'Dice Roler Bot',
($pass ? (Password => $pass) : ()),
);
$conn->add_handler('376',\&on_connect);
$conn->add_handler('msg',\&on_msg);
$conn->add_handler('public',\&on_public);
$irc->start;
sub on_connect{
my $self = shift;
for(@room){
print "Joining $_\n";
$self->join($_);
}
print "Connection phase complete... Waiting...\n";
}
sub on_public{
my $self = shift;
my $event = shift;
my $cmd = $event->{'args'}[0];
return unless($cmd =~ m/^~(\d+)d(\d+)(?:(?:(?=-)|\+)(-?\d+))?$/);
my @list;
print "Rolling...\n";
if($1 > 50){
$self->privmsg($event->{'to'}[0],"Error: You cannot roll more th
+an 50 dice.");
return;
}
my $total = $3; #Set the bonus to the base!
for(1..$1){
my $num = int(rand($2)+1);
$total += $num;
push(@list,$num);
}
$self->privmsg($event->{'to'}[0],$event->{'nick'}.
' rolled '.
$1.'d'.$2.($3 ? ($3 > 0 ? "+$3" : "$3") : '').
' for '.
join(', ',@list).
": $total"
);
}
sub on_msg{
my $self = shift;
my $event = shift;
print "Private message detected... processing/explaining\n";
if($event->{'args'}[0] eq 'quit'){
warn($event->{'nick'});
exit;
}
$self->privmsg($event->{'nick'},"This is $name, a dice roller.");
$self->privmsg($event->{'nick'},"Summery of use: type ~xdy+b into t
+he chat and I will roll the dice as specified, adding b to the total
+and reporting the results.");
}
__END__
=head1 NAME
D&D - Dice Roller
=head1 SYNOPSIS
perl irc.pl [options] server room
Options:
--help|-h brief help message
--port|-p=PORT# specify the port
--versions|-v version information
--name|n=botname specify the name of the bot
--pass|p=password specify a password
=head1 OPTIONS
=over 8
=item B<--help>
Prints a brief help message and then quits
=item B<--port>
Allows for specifying the port.
=item B<--versions>
Prints out a list of versions for the modules used by the program, and
+ the
program itself.
=item B<--name>
Specify a name for the bot
=item B<--pass>
If the server is password protected, set this.
=back
=head1 DESCRIPTION
B<This program> connects to the specified server and joins the request
+ed room.
Once connected, it waits for anyone in that room to say "~xdy+b" each
+variable
representing the following.
=over 8
=item B<x>
B<x> = the number of dice
=item B<d>
B<d> = a seperator common to D&D and other dice-based games.
=item B<y>
B<y> = the number of sides on the dice
=item B<b>
B<b> = something to arbitraraly add to the sum of the rolls, it is not
+ necessary
=back
This program will then reply to the current chat-room something to the
+ effect
of "E<lt>NAMEE<gt> rolls 4d10+2 and got the following: 4, 6, 10, 9: 31
+"
This program automatically terminates when private messaged the word "
+quit".
=head1 AUTHOR
This program was written by Stephen "Flame" Couchman E<lt>guildms@user
+s.sourceforge.netE<gt>
=head1 COPYRIGHT and REDISTRIBUTION
This program is copyright 2003 Stephen Couchman and may be freely modi
+fied and redistributed under the same terms as Perl itself.
=cut
Edit: Some changes made to properly apply the bonus, now only applys to the total rather than to each roll... thought it looked a little funny. Also expanded docs a little more... could still be better documented but for now...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.