-
Notifications
You must be signed in to change notification settings - Fork 8
/
dicebot.pl
executable file
·107 lines (86 loc) · 2.63 KB
/
dicebot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl -w
#
# A simple example of a bot that listens for and responds to on-channel
# user input. You can say "roll 2d12 + 2" or whatever, and it'll roll
# the appropriate number and type of dice and speak the result out loud.
#
# -- dennis taylor, <dennis@funkplanet.com>
use strict;
use POE;
use POE::Component::IRC;
use Data::Dumper;
my $nick = 'dicebot';
sub _start {
my ($kernel) = $_[KERNEL];
$kernel->post( 'dicebot', 'register', 'all');
$kernel->post( 'dicebot', 'connect', { Debug => 1,
Nick => $nick,
Server => $ARGV[0] ||
'scissorman.phreeow.net',
Port => $ARGV[1] || 6667,
Username => 'neenio',
Ircname => "HELP I'M A ROCK", },
);
$kernel->sig( INT => "sigint" );
}
sub irc_001 {
my ($kernel) = $_[KERNEL];
$kernel->post( 'dicebot', 'mode', $nick, '+i' );
$kernel->post( 'dicebot', 'join', '#dice' );
$kernel->post( 'dicebot', 'privmsg', '#dice', 'I am a dice-rolling bot.' );
$kernel->post( 'dicebot', 'topic', '#dice' );
}
sub irc_disconnected {
my ($server) = $_[ARG0];
print "Lost connection to server $server.\n";
$_[KERNEL]->post( "dicebot", "unregister", "all" );
}
sub irc_error {
my $err = $_[ARG0];
print "Server error occurred! $err\n";
}
sub irc_socketerr {
my $err = $_[ARG0];
print "Couldn't connect to server: $err\n";
$poe_kernel->sig( 'INT' );
}
sub sigint {
my $kernel = $_[KERNEL];
$kernel->post( 'dicebot', 'quit', 'Neenios on ice!' );
print "Tickles!!!!!!\n";
$kernel->sig( 'INT' );
$kernel->sig_handled();
}
sub _stop {
my ($kernel) = $_[KERNEL];
print "Control session stopped.\n";
}
sub irc_public {
my ($kernel, $who, $chan, $msg) = @_[KERNEL, ARG0 .. ARG2];
$who =~ s/^(.*)!.*$/$1/ or die "Weird-ass who: $who";
my ($num, $die, $plus) =
($msg =~ /^\s*(?:$nick,?\s*)?roll (?:a )?(\d+)d(\d+)\s*([+-]\s*(\d+))?/i);
return unless $num and $die and $num < 1000 and $die < 10000;
$plus =~ tr/+ //d if $plus;
my @rolls;
my $sum = $plus || 0;
for (1 .. $num) {
push @rolls, int rand( $die ) + 1;
$sum += $rolls[-1];
}
my $str = "You rolled a $sum";
if (@rolls > 1 or $plus) {
$plus = '' unless $plus;
$plus =~ s/\-(\d)/ \- $1/;
$plus =~ s/^(\d)/ \+ $1/;
$str .= " (" . join( " + ", @rolls) . "$plus)";
}
$kernel->post( 'dicebot', 'privmsg', $chan, "$who: $str" );
}
POE::Component::IRC->new( 'dicebot' ) or
die "Can't instantiate new IRC component!\n";
POE::Session->create( package_states => [
'main' => [qw(_start _stop irc_001 irc_disconnected sigint
irc_socketerr irc_error irc_public)],], );
$poe_kernel->run();
exit 0;