Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use Net::IRC;
- $server = 'irc.magicstar.net';
- $channel = '#GWAMS';
- $botnick = 'PainBot';
- $botuname = 'painbot';
- $password = 'foobar';
- $botadmin = 'AnnoDomini';
- $comment = 'AD\'s Other DiceBot';
- $version = '0.26-271110';
- $maxline = 400;
- $irc = new Net::IRC;
- $conn = $irc->newconn(Nick => $botnick,
- Server => $server,
- Port => 6667,
- Username => $botuname,
- Ircname => $comment);
- $conn->add_global_handler('376', \&on_connect);
- $conn->add_global_handler('disconnect', \&on_disconnect);
- $conn->add_global_handler('kick', \&on_kick);
- $conn->add_global_handler('msg', \&on_priv);
- $conn->add_global_handler('cversion', \&on_ctcp_version);
- $conn->add_global_handler('public', \&on_public);
- $conn->add_global_handler('invite', \&on_invite);
- $conn->add_global_handler('cjoin', \&on_ctcp_join);
- $conn->add_global_handler('cpart', \&on_ctcp_part);
- print STDERR "PainBot v".$version." started.\n";
- $irc->start;
- sub on_ctcp_join {
- $self = shift;
- $event = shift;
- foreach $arg ($event->args) {
- $chan = $arg;
- $self->join($chan);
- print STDERR "Joining $chan.\n";
- }
- }
- sub on_ctcp_part {
- $self = shift;
- $event = shift;
- foreach $arg ($event->args) {
- $self->part($chan);
- print STDERR "Parting $chan.\n";
- }
- }
- sub on_connect {
- $self = shift;
- $self->privmsg('nickserv', "identify $password");
- $self->join($channel);
- print STDERR "Connected to " . $server . ".\n";
- }
- sub on_disconnect {
- $self = shift;
- $self->connect();
- }
- sub on_kick {
- $self = shift;
- $event = shift;
- print STDERR "Kicked by ".$event->nick.".\n";
- }
- sub on_priv {
- $self = shift;
- $event = shift;
- handle_message($self,$event,$event->nick);
- }
- sub on_public {
- $self = shift;
- $event = shift;
- handle_message($self,$event,$event->to);
- }
- sub handle_message {
- $self = shift;
- $event = shift;
- $dest = shift;
- foreach $arg ($event->args) {
- if (($arg =~ /^!die/i) && ($event->nick eq $botadmin)) {
- $self->quit("I return to the Wheel of Suffering.");
- print STDERR "Received shutdown command. Exiting.\n";
- exit 0;
- } elsif ($arg =~ /^!ping/i) {
- $self->privmsg($dest, 'pong!');
- print STDERR "Ping command activated by ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!version/i) {
- $self->privmsg($dest, 'PainBot v' . $version . ' by AnnoDomini.');
- print STDERR "Version command activated by ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!roll\s+/i) {
- $expression = substr($arg,6,$maxline);
- command_roll($self,$dest,$event->nick,$expression);
- print STDERR "Rolling $expression for ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!help/i) {
- $self->privmsg($event->nick, "You have reached the help function of the PainBot, v$version, by AnnoDomini. The bot is under construction, and more help is unavailable.");
- print STDERR "Help requested by ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!exalted\s+/i) {
- $expression = substr($arg,9,$maxline);
- command_exalted($self,$dest,$event->nick,$expression);
- print STDERR "Rolling Exalted dice for ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!sr3\s+/i) {
- $expression = substr($arg,5,$maxline);
- command_sr3($self,$dest,$event->nick,$expression);
- print STDERR "Rolling SR3 dice for ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!nwod\s/i) {
- $expression = substr($arg,6,$maxline);
- command_nwod($self,$dest,$event->nick,$expression);
- print STDERR "Rolling nWoD dice for ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!space\s+/i) {
- $expression = substr($arg,7,$maxline);
- command_space($self,$dest,$event->nick,$expression);
- print STDERR "Rolling Space dice for ".$event->nick." in $dest.\n";
- } elsif ($arg =~ /^!join\s+/i) {
- $expression = substr($arg,6,$maxline);
- command_join($self,$expression);
- print STDERR "Join request in $dest.\n";
- } elsif ($arg =~ /^!part\s+/i) {
- $expression = substr($arg,6,$maxline);
- command_part($self,$expression);
- print STDERR "Part request in $dest.\n";
- } else {
- if (($arg =~ /^!d/i) || ($arg =~ /^!\d/i)) { # crude but effective
- $expression = substr($arg,1,$maxline);
- command_roll($self,$dest,$event->nick,$expression);
- print STDERR "Rolling $expression for ".$event->nick." in $dest.\n";
- }
- }
- }
- }
- sub command_part {
- $self = shift;
- $chan = shift;
- $self->part($chan);
- print STDERR "Parting " . $chan . ".\n";
- }
- sub command_join {
- $self = shift;
- $chan = shift;
- $self->join($chan);
- print STDERR "Joining " . $chan . ".\n";
- }
- sub on_ctcp_version {
- $self = shift;
- $event = shift;
- $self->ctcp_reply($event->nick,'PainBot v' . $version . ' by AnnoDomini.');
- print STDERR "Received and replied to CTCP VERSION from " . $event->nick . ".\n";
- }
- sub command_roll {
- $self = shift;
- $dest = shift;
- $nick = shift;
- $args = shift;
- $output = "[$nick] rolled ";
- $totals = "";
- if ($args =~ /:/) {
- $colonpos = index $args,':';
- $comment = trim(substr($args,$colonpos+1,$maxline));
- $expressionwithreps = substr($args,0,$colonpos);
- } else {
- $comment = $args;
- $expressionwithreps = $args;
- }
- $output .= "\"$comment\": ";
- if ($expressionwithreps =~ /,/) {
- $commapos = index $expressionwithreps,',';
- $repetitions = alltrim(substr($expressionwithreps,$commapos+1,$maxline));
- $expression = alltrim(substr($expressionwithreps,0,$commapos));
- } else {
- $repetitions = 1;
- $expression = alltrim($expressionwithreps);
- }
- $expression =~ s/\^/\*\*/; # legacy
- if ($repetitions < 0) { $repetitions = 1; }
- if ($repetitions > 30) { $repetitions = 30; }
- $explength = length($expression);
- $expcopy = $expression;
- $mathableresult = "";
- for ($rep = 0;$rep<$repetitions;$rep++) { # rep loop
- $lastoperator = -1;
- for ($pos = 0;$pos<$explength;$pos++) { # string parsing
- $curchar = substr($expression,$pos,1);
- if (is_operator($curchar) == 1 || $pos == $explength-1) {
- if ($pos == $explength-1) {
- $mathableresult .= parse_one_roll(substr($expression,$lastoperator+1,$maxline));
- } else {
- $mathableresult .= parse_one_roll(substr($expression,$lastoperator+1,$pos-$lastoperator-1)) . $curchar;
- }
- $lastoperator = $pos;
- }
- }
- $output .= "$mathableresult, ";
- $totals .= eval($mathableresult)." ";
- $mathableresult = "";
- }
- $output = substr($output,0,-2).". Total: \x02".substr($totals,0,-1)."\x02.";
- $self->privmsg($dest, $output);
- }
- sub is_operator {
- $input = shift;
- if ($input eq '+') { return 1; }
- if ($input eq '-') { return 1; }
- if ($input eq '*') { return 1; }
- if ($input eq '/') { return 1; }
- if ($input eq '^') { return 1; }
- if ($input eq '%') { return 1; }
- return 0;
- }
- sub parse_one_roll {
- $args = shift;
- $dpos = index lc($args),'d';
- if ($dpos == -1) { return $args; }
- if ($args =~ /l/i) { # l
- $droplow = 1;
- $args =~ s/l//ig;
- } else {
- $droplow = 0;
- }
- if ($args =~ /h/i) { # h
- $drophigh = 1;
- $args =~ s/h//ig;
- } else {
- $drophigh = 0;
- }
- if ($args =~ /f/i) { # f
- $floating = 1;
- $args =~ s/f//ig;
- } else {
- $floating = 0;
- }
- if ($dpos == 0) {
- $nrofdice = 1;
- } else {
- $nrofdice = substr($args,0,$dpos);
- if ($nrofdice == 0) {
- $nrofdice = 1;
- }
- }
- $diesize = substr($args,$dpos+1,$maxline);
- $result = '('; $highest = 0; $lowest = $diesize+1;
- for ($i = 0;$i<$nrofdice;$i++) {
- do {
- $current = int(rand($diesize))+1;
- if (($droplow == 1) && ($current < $lowest)) { $lowest = $current; }
- if (($drophigh == 1) && ($current > $highest)) { $highest = $current; }
- $result .= $current . '+';
- } while (($current == $diesize) && ($floating == 1));
- }
- $result = substr($result,0,-1);
- if ($droplow == 1) { $result = $result."-$lowest"; }
- if ($drophigh == 1) { $result = $result."-$highest"; }
- $result = $result.')';
- return $result;
- }
- sub trim($) {
- my $string = shift;
- $string =~ s/^\s+//;
- $string =~ s/\s+$//;
- return $string;
- }
- sub ltrim($) {
- my $string = shift;
- $string =~ s/^\s+//;
- return $string;
- }
- sub rtrim($) {
- my $string = shift;
- $string =~ s/\s+$//;
- return $string;
- }
- sub alltrim($) {
- my $string = shift;
- $string =~ s/\s+//g;
- return $string;
- }
- sub one_shift {
- $shifted = shift;
- return $shifted;
- }
- sub on_invite {
- $self = shift;
- $event = shift;
- $chan = one_shift($event->args);
- $self->join($chan);
- print STDERR "Received invite from " . $event->nick . " to ".$chan.". Joining.\n";
- }
- sub command_exalted {
- $self = shift;
- $dest = shift;
- $nick = shift;
- $args = shift;
- $output = "[$nick] rolled ";
- if ($args =~ /:/) {
- $colonpos = index $args,':';
- $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
- $exaltedexpression = substr($args,0,$colonpos);
- } else {
- $comment = "";
- $exaltedexpression = $args;
- }
- ($dice,$tn,$reps,$ext) = split(/ /,$exaltedexpression);
- if (($dice =~ /a/i) || ($dice =~ /b/i) || ($dice =~ /l/i) || ($dice =~ /m/i)) {
- $tensexplode = 0;
- } else {
- $tensexplode = 1;
- }
- if ($dice =~ /f/i) {
- $flurry = 1;
- } else {
- $flurry = 0;
- }
- $dice =~ s/a//ig; $dice =~ s/m//ig; $dice =~ s/l//ig; $dice =~ s/b//ig; $dice =~ s/f//ig;
- $temp = "("; $successes = "";
- if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
- if ($reps > 30) { $reps = 30; }
- if ($dice > 200) { $dice = 200; }
- if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
- if (length($tn) == 0) { $tn = 7; }
- if ($tn < 1) { $tn = 1; }
- if ($tn > 10) { $tn = 10; }
- if (length($ext) == 0) { $ext = 0; }
- for ($rep = 0;$rep<$reps;$rep++) {
- $currentsuccesses = 0 + $ext;
- for ($die = 0;$die<$dice;$die++) {
- $current = int(rand(10))+1;
- if ($current >= $tn) {
- $currentsuccesses++;
- if (($tensexplode == 1) && ($current == 10)) {
- $currentsuccesses++;
- }
- }
- $temp = $temp . $current . " ";
- }
- $successes = $successes . $currentsuccesses . "; ";
- $temp = substr($temp,0,-1) . "; ";
- if ($flurry == 1) {
- $dice = $dice - 1;
- }
- }
- $temp = substr($temp,0,-2) . ")";
- $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
- $self->privmsg($dest,$output);
- }
- sub command_sr3 {
- $self = shift;
- $dest = shift;
- $nick = shift;
- $args = shift;
- $output = "[$nick] rolled ";
- if ($args =~ /:/) {
- $colonpos = index $args,':';
- $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
- $sr3expression = substr($args,0,$colonpos);
- } else {
- $comment = "";
- $sr3expression = $args;
- }
- ($dice,$tn,$reps) = split(/ /,$sr3expression);
- $temp = "("; $successes = "";
- if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
- if ($reps > 30) { $reps = 30; }
- if ($dice > 200) { $dice = 200; }
- if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
- if (length($tn) == 0) { $tn = 4; }
- if ($tn < 1) { $tn = 1; }
- if ($tn > 10) { $tn = 10; }
- for ($rep = 0;$rep<$reps;$rep++) {
- $currentsuccesses = 0;
- for ($die = 0;$die<$dice;$die++) {
- $current = 0;
- do {
- $now = int(rand(6))+1;
- $current = $current + $now;
- } while ($now == 6);
- if ($current >= $tn) {
- $currentsuccesses++;
- }
- $temp = $temp . $current . " ";
- }
- $successes = $successes . $currentsuccesses . "; ";
- $temp = substr($temp,0,-1) . "; ";
- }
- $temp = substr($temp,0,-2) . ")";
- $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
- $self->privmsg($dest,$output);
- }
- sub command_nwod {
- $self = shift;
- $dest = shift;
- $nick = shift;
- $args = shift;
- $output = "[$nick] rolled ";
- if ($args =~ /:/) {
- $colonpos = index $args,':';
- $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
- $nwodexpression = substr($args,0,$colonpos);
- } else {
- $comment = "";
- $nwodexpression = $args;
- }
- ($dice,$reps) = split(/ /,$nwodexpression);
- if ($dice =~ /n/) {
- $tensexplode = 0;
- } else {
- $tensexplode = 1;
- }
- if ($dice =~ /c/) {
- $tn = 10;
- } else {
- $tn = 8;
- }
- $dice =~ s/n//ig; $dice =~ s/c//ig;
- $temp = "("; $successes = "";
- if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
- if ($reps > 30) { $reps = 30; }
- if ($dice > 200) { $dice = 200; }
- if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
- for ($rep = 0;$rep<$reps;$rep++) {
- $currentsuccesses = 0;
- for ($die = 0;$die<$dice;$die++) {
- if ($tensexplode == 1) {
- do {
- $current = int(rand(10))+1;
- if ($current >= $tn) {
- $currentsuccesses++;
- }
- $temp = $temp . $current . " ";
- } while ($current == 10);
- } else {
- $current = int(rand(10))+1;
- if ($current >= $tn) {
- $currentsuccesses++;
- }
- $temp = $temp . $current . " ";
- }
- }
- $successes = $successes . $currentsuccesses . "; ";
- $temp = substr($temp,0,-1) . "; ";
- }
- $temp = substr($temp,0,-2) . ")";
- $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
- $self->privmsg($dest,$output);
- }
- sub command_space {
- $self = shift;
- $dest = shift;
- $nick = shift;
- $args = shift;
- $output = "[$nick] rolled ";
- if ($args =~ /:/) {
- $colonpos = index $args,':';
- $comment = "\"". trim(substr($args,$colonpos+1,$maxline)) ."\": ";
- $spaceexpression = substr($args,0,$colonpos);
- } else {
- $comment = "";
- $spaceexpression = $args;
- }
- ($dice,$reps) = split(/ /,$spaceexpression);
- $sixesexplode = 1;
- $tn = 4;
- $temp = "("; $successes = "";
- if (($reps < 1) || (length($reps) == 0)) { $reps = 1; }
- if ($reps > 30) { $reps = 30; }
- if ($dice > 200) { $dice = 200; }
- if (($dice < 1) || (length($dice) == 0)) { $dice = 1; }
- for ($rep = 0;$rep<$reps;$rep++) {
- $currentsuccesses = 0;
- for ($die = 0;$die<$dice;$die++) {
- if ($sixesexplode == 1) {
- do {
- $current = int(rand(6))+1;
- if ($current >= $tn) {
- $currentsuccesses++;
- }
- if ($current == 6) {
- $temp = $temp . "\x02" . $current . "\x02 ";
- } else {
- $temp = $temp . $current . " ";
- }
- } while ($current == 6);
- } else {
- $current = int(rand(6))+1;
- if ($current >= $tn) {
- $currentsuccesses++;
- }
- if ($current == 6) {
- $temp = $temp . "\x02" . $current . "\x02 ";
- } else {
- $temp = $temp . $current . " ";
- }
- }
- }
- $successes = $successes . $currentsuccesses . "; ";
- $temp = substr($temp,0,-1) . "; ";
- }
- $temp = substr($temp,0,-2) . ")";
- $output = $output . $comment . $temp . ". Successes (TN " . $tn . ") = \x02" . substr($successes,0,-2) . "\x02.";
- $self->privmsg($dest,$output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement