Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

IRC Dice Roller

by Flame (Deacon)
on May 12, 2003 at 20:47 UTC ( #257562=sourcecode: print w/replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info

This program is an IRC dice roller that connects to an IRC server and joins a room specified on the command line. Once connected, the program waits for someone to say a slightly modified form of the D&D dice format: "~xdy+b". Here, x is the number of numbers to generate, y is the max number, eg: 1..y and b is an optional number to add to the total. When it recieves this, it generates the numbers and spits out a list of each number it generated, followed by their sum.

It was originally written for and tested with the following, but barring any significant changes in interface, it should work cross-platform and with any newer versions of the modules:

Net::IRC        0.73
Pod::Usage      1.14
Getopt::Long    2.32
strict          1.02
Perl            5.008
OS              MSWin32

Further documentation can be found at the end of the program as POD.

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";

   'help|h' => \$help,
   'port|p=i' => \$port,
   'versions|v' => \$versions,
   'pass|p=s'   => \$pass,
   'name|n=s'   => \$name,
) or (pod2usage() && die);


   "\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",
   "          $VERSION\n",
   "  $0\n\n";

   local $@;
      $server = shift(@ARGV) or (pod2usage() && die);
      @room = @ARGV or (pod2usage() && die);
   if($@ =~ m#Can't open -e for reading#){
      #Used with tinyperl

my $irc = new Net::IRC;
my $conn = $irc->newconn(
   Nick => $name,
   Server => $server,
   Port => $port,
   Ircname => 'Dice Roler Bot',
   ($pass ? (Password => $pass) : ()),



sub on_connect{
   my $self = shift;

      print "Joining $_\n";
   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.");

   my $total = $3; #Set the bonus to the base!
      my $num = int(rand($2)+1);
      $total += $num;
   ' 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'){
   $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.");


=head1 NAME

D&D - Dice Roller


perl [options] server room

       --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.



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 
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


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 "

=head1 AUTHOR

This program was written by Stephen "Flame" Couchman E<lt>guildms@user


This program is copyright 2003 Stephen Couchman and may be freely modi
+fied and redistributed under the same terms as Perl itself.

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...
Replies are listed 'Best First'.
Re: IRC Dice Roller
by mojotoad (Monsignor) on May 12, 2003 at 21:42 UTC

      True some of these are fascinating, though I must say I am quite happy I did not use Games::Die::Dice... an object oriented dice roller seems overkill to me, especially since I can never predict the sides of the dice I'll need and I don't want to have to constantly call 'new'. Games::Dice would have had more potential and, I admit, it did point out one thing I forgot... the bonus is typically applied to the sum only, not to each roll. I'll probably update this with that corrected shortly.

      Thanks for pointing these out.

      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://257562]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-09-27 11:01 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (142 votes). Check out past polls.