All 6 entries tagged IRC

View all 10 entries tagged IRC on Warwick Blogs | View entries tagged IRC at Technorati | View all 1 images tagged IRC

June 28, 2006

DiceyBOT continued

Follow-up to DiceyBOT from Nothing to see here

To finish what I started last night, here is the source of DiceyBOT, written (slightly) more sensibly. Arguably, there are still bits which could be written more verbosely, or which would send people scurrying for the regex documentation.
#!/usr/bin/perl

use strict;
use warnings;

use Net::IRC;

my $channel = '#bots';
my $irc = new Net::IRC;

my $conn = $irc->newconn(
Nick => 'DiceyBOT',
Server => 'uwcs.co.uk',
);

$conn->add_handler(376, \&on_connect);
$conn->add_handler('public', \&on_public);

$irc->start;

sub on_connect {
my $self = shift;
$self->join($channel);
}

sub on_public {
my ($self, $event) = @_;

$_ = ($event->args)[0];

return unless /^-roll/;

my @answers;
while (/\G\D*(\d*)d(\d+)/ig) {
my $sum;
for (1..($1||1)) {
$sum += 1+int(rand($2));
}
push @answers, $sum;
}

my $nick = (split /!/, $event->from)[0];

$self->privmsg(
$channel,
"$nick: You rolled " . join(" + ", @answers)
) if @answers;
}

1;
It does the same as Skumby's Java version, but without the bug where it prints words. It is shorter, faster, uses less memory (~7MB virtual, 4MB resident), and was quicker to develop. I'm not saying Java sucks; I'm saying that Perl is the right (or at least, a better) tool for the job, where the job is a short, simple IRC bot.

This blog is obsolete

From 20th August 2007, this blog is no longer being updated. For something more recent, please visit Tim Retout’s website.

Search this blog

Not signed in
Sign in

Powered by BlogBuilder
© MMXXI