Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 13.93 KB | None | 0 0
  1. #!/usr/bin/perl
  2. print "POE IRC Bot. Skeleton by Caaz.\n";
  3. print "You can add an optional NICK.\n";
  4. print "SERVER (Optional PORT NICK)\n";
  5. chomp($Connect = <STDIN>);
  6. # Thursday July 8th
  7. use POE::Component::IRC;
  8. use POE;
  9.  
  10. BOTVARS: {
  11.     $BotNick = "PerlBot";       # Nick
  12.     $BotUser = ".";     # Username "indent"
  13.     $BotName = "a Perl bot. Luka 3";    # Real Name
  14.     $BotPass = "Password";      # Nickserv Password
  15.     $BotOwner = "Caaz";     # Owner of the Bot
  16.     $BotPath = "/Users/Caaz/Desktop/Perl/BOT/";     # Path to any files.
  17. }
  18. FILES: {
  19.     open DATA, "<".$BotPath."DBUsers.txt";
  20.     @DB = <DATA>;
  21.     chomp($DBUsers = $DB[0]);
  22.     close DATA;
  23. }
  24. open DATA, "<".$BotPath.(split / /, $Connect)[0]."-AutoJoin.txt";
  25. @Channels=<DATA>;
  26. close DATA;chomp(@Channels);
  27. $BotChannels = $Channels[0];
  28. $Connect = $Connect." ".$BotNick;
  29. $BotNick = (split / /, $Connect)[2];
  30. POE::Session->create(inline_states => {
  31.     _start     => sub {
  32.         $irc->yield(register => "all");
  33.         $irc->yield( connect => {
  34.             Nick     => (split / /, $Connect)[2],
  35.             Username => $BotUser,
  36.             Ircname  => $BotName,
  37.             Server   => (split / /, $Connect)[0],
  38.             Port     => (split / /, $Connect)[1],
  39.         });
  40.     },
  41.     irc_001 => sub {
  42.         $irc->yield(mode => $BotNick." +B");
  43.         $irc->yield(privmsg => "Nickserv" => "id ".$BotPass);
  44.         my $alarm_id = $irc->delay( [ join => $BotChannels ], 2 );
  45.     },
  46.     irc_error => sub {
  47.         my ($kernel, $IRCWhat) = @_[KERNEL, ARG0];
  48.         print $IRCWhat."\n";
  49.     },
  50.     irc_ctcp_action => sub {
  51.         my ($kernel, $IRCWho, $where, $IRCWhat) = @_[KERNEL, ARG0, ARG1, ARG2];
  52.         $IRCWhere = $where->[0];
  53.         print GetTime()."$IRCWhere >    * ".(split /!/, $IRCWho)[0]." $IRCWhat\n";
  54.         Bot("ACTION",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere,$IRCWhat);
  55.     },
  56.     irc_join => sub {
  57.         my ($kernel, $IRCWho, $IRCWhere) = @_[KERNEL, ARG0, ARG1];
  58.         print GetTime()."Join       * ".(split /!/, $IRCWho)[0]." joins $IRCWhere\n";
  59.         Bot("JOIN",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere);
  60.     },
  61.     irc_invite => sub {
  62.         my ($kernel, $IRCWho, $IRCWhere) = @_[KERNEL, ARG0, ARG1];
  63.         print GetTime()."Invi       * ".(split /!/, $IRCWho)[0]." invites you to $IRCWhere\n";
  64.         Bot("INVITE", (split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere);
  65.     },
  66.     irc_kick => sub {
  67.         my ($kernel, $IRCWho, $IRCWhere, $IRCWhoElse, $IRCWhat) = @_[KERNEL, ARG0, ARG1, ARG2, ARG3];
  68.         print GetTime()."Kick       * ".(split /!/, $IRCWho)[0]." kicks $IRCWhoElse out of $IRCWhere ($IRCWhat)\n";
  69.         Bot("KICK",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere,$IRCWhoElse,$IRCWhat);
  70.     },
  71.     irc_msg => sub {
  72.         my ($kernel, $IRCWho, $where, $IRCWhat) = @_[KERNEL, ARG0, ARG1, ARG2];
  73.         $IRCWhere = $where->[0];
  74.         print GetTime()."Priv\a>        <".(split /!/, $IRCWho)[0].">   $IRCWhat\n";
  75.         Bot("PRIVMSG",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere,$IRCWhat);
  76.     },
  77.     irc_nick => sub {
  78.         my ($kernel, $IRCWho, $IRCWhoElse) = @_[KERNEL, ARG0, ARG1];
  79.         print GetTime()."Nick       * ".(split /!/, $IRCWho)[0]." changes nick to $IRCWhoElse\n";
  80.         Bot("NICK", (split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhoElse);
  81.     },
  82.     irc_notice => sub {
  83.         my ($kernel, $IRCWho, $where, $IRCWhat) = @_[KERNEL, ARG0, ARG1, ARG2];
  84.         $IRCWhere = $where->[0];
  85.         $BotOwner = $IRCWho if($IRCWhat =~ /^BBB$/);
  86.         print GetTime()."Noti       <".(split /!/, $IRCWho)[0].">   $IRCWhat\n" if((split /!/, $IRCWho)[0] !~ /WolfBot/i);
  87.         Bot("NOTICE",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere,$IRCWhat);
  88.     },
  89.     irc_part => sub {
  90.         my ($kernel, $IRCWho, $IRCWhere, $IRCWhat) = @_[KERNEL, ARG0, ARG1, ARG2];
  91.         exit 1 if($IRCWhat =~ /BBB/);
  92.         print GetTime()."Part       * ".(split /!/, $IRCWho)[0]." left channel $IRCWhere \(".$IRCWhat."\)\n";
  93.         Bot("PART",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere,$IRCWhat);
  94.     },
  95.     irc_public => sub {
  96.         my ($kernel, $IRCWho, $where, $IRCWhat) = @_[KERNEL, ARG0, ARG1, ARG2];
  97.         $IRCWhere = $where->[0];
  98.         print GetTime()."$IRCWhere >    <".(split /!/, $IRCWho)[0].">   $IRCWhat\n";
  99.         Bot("CHANMSG",(split /!/, $IRCWho)[0],(split /!/, $IRCWho)[1],$IRCWhere,$IRCWhat);
  100.     },
  101.     irc_disconnected => sub { print "Disconnected.\n"; exit 1; }, # Ends on disconnect.
  102. },);
  103. sub Bot {
  104.     $Event = $_[0];
  105.     if($Event =~ /CHANMSG/) { $Com = "privmsg ".$_[3]." "; $UserMsg = $_[4]; $Where = $_[3]; }
  106.     elsif($Event =~ /NOTICE/) { $Com = "notice ".$_[1]." "; $UserMsg = $_[4]; $Where = $_[1]; }
  107.     elsif($Event =~ /PRIVMSG/) { $Com = "notice ".$_[1]." "; $UserMsg = $_[4]; $Where = $_[1]; }
  108.     elsif($Event =~ /INVITE/) { $Com = "notice ".$_[1]." "; $UserMsg = $_[3]; $Where = $_[1]; if ($UserMsg !~ /Spp/i) { $irc->yield(join => $UserMsg); }} # Joins on invite, if it's not spp. heh.
  109.     elsif($Event =~ /JOIN/) { $Com = "notice ".$_[3]." "; $UserMsg = $_[3]; $Where = $_[1]; }
  110.     elsif($Event =~ /NICK/) { if($_[1] =~ /^$BotOwner$/i) { print "Owner changing...\n"; $BotOwner = Regexit($_[3]) } }
  111.     $UserNick = Regexit($_[1]);
  112.     $UserHost = $_[2];
  113.     $UserMsg = POE::Component::IRC::Common::strip_formatting(POE::Component::IRC::Common::strip_color($UserMsg));
  114.     $CurSec = SpamProtect();
  115.     &CREATEUser($UserNick);
  116.     if($DBUsers =~ /(.*)<$UserNick :(.*?)\[Access (.*?)\](.*?)>(.*)/i) {
  117.         $Access = $3;
  118.         if($Access <= 0) {
  119.             return 0;
  120.         }
  121.     }
  122.     if(($Where =~ /wwolf/i) || ($UserNick =~ /Wolfbot/i)) {
  123.         if($Event =~ /(CHANMSG|NOTICE|PRIVMSG)/) {
  124.             if(($UserMsg =~ /^($BotNick|Bots), Join/i) && ($WW{"Will Join"} !~ /No/i)) {
  125.                 $irc->yield(privmsg => "#WWolf" => "!Join");
  126.                 $WW{"Will Join"} = "No";
  127.                 $WW{"MyType"} = "Villager";
  128.             }
  129.             elsif(($UserMsg =~ /^$BotNick, Leave/i) && ($WW{"Will Join"} =~ /No/i)) {
  130.                 $irc->yield(part => "#WWolf" => "Invite me, and I will continue playing.");
  131.                 $WW{"Will Join"} = "Yes";
  132.                 while ( ($key, $value) = each %WW ) {
  133.                     $WW{$key} = "";
  134.                 }
  135.             }
  136.             elsif($UserMsg =~ /^$BotNick won,/i) {
  137.                 my $alarm_id = $irc->delay( [ privmsg => "#wwolf" => "Woohoo! I won. :D" ], 1 );
  138.                 while ( ($key, $value) = each %WW ) {
  139.                     $WW{$key} = "";
  140.                 }
  141.             }
  142.             elsif($UserMsg =~ /^$BotNick lost,/i) {
  143.                 my $alarm_id = $irc->delay( [ privmsg => "#wwolf" => ";~; I didn't think I'd lose..." ], 1 );
  144.                 while ( ($key, $value) = each %WW ) {
  145.                     $WW{$key} = "";
  146.                 }
  147.             }
  148.             elsif($UserMsg =~ /^The game is over\.$|^Game cancelled\./i) {
  149.                 while ( ($key, $value) = each %WW ) {
  150.                     $WW{$key} = "";
  151.                 }
  152.             }
  153.             elsif($UserMsg =~ /^($BotNick|Bots), what are you/i) {
  154.                 if($WW{"MyType"} =~ /Wolf|Village Idiot/) {
  155.                     $irc->yield(privmsg => "#WWolf" => "I am a Villager.");
  156.                 }
  157.                 else {
  158.                     $irc->yield(privmsg => "#WWolf" => "I am a ".$WW{"MyType"}.".");
  159.                 }
  160.             }
  161.             elsif($UserMsg =~ /(.*?) (has joined|has started)/i) {
  162.                 if($1 =~ /$BotNick/) { return 0; }
  163.                 $WW{"Players"} = $WW{"Players"}.$1." ";
  164.                 @WWPlayers = split / /, $WW{"Players"};
  165.                 print "Updating players...\n".@WWPlayers."\n";
  166.             }
  167.             elsif($UserMsg =~ /^(.*) (has left the game|struggles vainly|was)/i) {
  168.                 $WW{"Players"} =~ s/$1 //igs;
  169.                 @WWPlayers = split / /, $WW{"Players"};
  170.                 print "Updating players...\n".@WWPlayers."\n";
  171.             }
  172.             elsif($UserMsg =~ /has cast their vote for (.*?)!/i) {
  173.                 $tring = $1;
  174.                 $WW{"Votes".Regexit($tring)}++;
  175.                 if(($WW{"Votes".Regexit($tring)} > $WW{"MostVoted"}) && (Regexit($tring) !~ /$WW{"MostVotedName"}/)) {
  176.                     $WW{"MostVoted"} = $WW{"Votes".Regexit($tring)};
  177.                     $WW{"MostVotedName"} = Regexit($tring);
  178.                     $irc->yield(privmsg => "WolfBot" => "vote ".$tring) if(int(rand(100)) > 60);
  179.                 }
  180.             }
  181.             elsif($UserMsg =~ /seconds to send your votes/i) {
  182.                 if($WW{"Vote"} !~ /^$/) {
  183.                     my $alarm_id = $irc->delay( [ privmsg => "WolfBot" => "vote ".$WW{"Vote"} ], 5 );
  184.                 }
  185.                 $WW{"Vote"} = "";
  186.                 if($WW{"MyType"} =~ /Village Idiot/i) {
  187.                     $WW{"Vote"} = $BotNick;
  188.                 }
  189.             }
  190.             if(($UserNick =~ /WolfBot/i) && ($Event =~ /(NOTICE|PRIVMSG|CHANMSG)/)) {
  191.                 if($UserMsg =~ /You are a wolf!/i) {
  192.                     $WW{"MyType"} = "Wolf";
  193.                     print "Wolf!";
  194.                 }
  195.                 elsif($UserMsg =~ /You are the finder!/i) {
  196.                     $WW{"MyType"} = "Finder";
  197.                 }
  198.                 elsif($UserMsg =~ /You are the seer!/i) {
  199.                     $WW{"MyType"} = "Seer";
  200.                 }
  201.                 elsif($UserMsg =~ /You are the doppleganger!/i) {
  202.                     $WW{"MyType"} = "Doppleganger";
  203.                 }
  204.                 elsif($UserMsg =~ /You are the angel!/i) {
  205.                     $WW{"MyType"} = "Angel";
  206.                 }
  207.                 elsif($UserMsg =~ /You are the Village Idiot!/i) {
  208.                     $WW{"MyType"} = "Village Idiot";
  209.                     my $alarm_id = $irc->delay( [ privmsg => "#WWolf" => $WWPlayers[int(rand(@WWPlayers))]." is Wolf." ], 50 );
  210.                     $WW{"Vote"} = $BotNick;
  211.                 }
  212.                 elsif($UserMsg =~ /into (a|an) (.*?)!/i) {
  213.                     $tring = $2;
  214.                     if($tring =~ /werewolf/i) { $WW{"MyType"} = "Wolf"; }
  215.                     else {
  216.                         $WW{"MyType"} = $tring;
  217.                     }
  218.                 }
  219.                 elsif($UserMsg =~ /You have 45/i) {
  220.                     if($WW{"MyType"} =~ /Wolf/i) {
  221.                         $irc->yield(privmsg => "wolfbot" => "kill ".$WWPlayers[int(rand(@WWPlayers))]);
  222.                     }
  223.                     elsif($WW{"MyType"} =~ /Seer/i) {
  224.                         foreach (@WWPlayers) {
  225.                             if($WW{"Targeted"} !~ /Regexit($_)/i){
  226.                                 $WW{"Targeted"} = $WW{"Targeted"}.Regexit($_);
  227.                                 $WW{"Target"} = $_;
  228.                                 last;
  229.                             }
  230.                         }
  231.                         $irc->yield(privmsg => "wolfbot" => "see ".$WW{"Target"});
  232.                     }
  233.                     elsif($WW{"MyType"} =~ /Finder/i) {
  234.                         foreach (@WWPlayers) {
  235.                             if($WW{"Targeted"} !~ /Regexit($_)/i){
  236.                                 $WW{"Targeted"} = $WW{"Targeted"}.Regexit($_);
  237.                                 $WW{"Target"} = $_;
  238.                                 last;
  239.                             }
  240.                         }
  241.                         $irc->yield(privmsg => "wolfbot" => "find ".$WW{"Target"});
  242.                     }
  243.                     if($WW{"MyType"} =~ /Doppleganger/i) {
  244.                         $WW{"Target"} = $WWPlayers[int(rand(@WWPlayers))];
  245.                         $irc->yield(privmsg => "wolfbot" => "dopple ".$WW{"Target"});
  246.                     }
  247.                     if($WW{"MyType"} =~ /Angel/i) {
  248.                         $WW{"Target"} = $WWPlayers[int(rand(@WWPlayers))];
  249.                         $irc->yield(privmsg => "wolfbot" => "shield ".$WW{"Target"});
  250.                     }
  251.                 }
  252.                 elsif($UserMsg =~ /(.*?) is (a|an) (.*?)!/i) {
  253.                     $tring = $1;
  254.                     $tringt = $3;
  255.                     if($tringt =~ /Wolf/i) {
  256.                         my $alarm_id = $irc->delay( [ privmsg => "#WWolf" => $tring." is Wolf." ], 5 );
  257.                         $WW{"Vote"} = $tring;
  258.                     }
  259.                     if($tringt =~ /Idiot/i) {
  260.                         my $alarm_id = $irc->delay( [ privmsg => "#WWolf" => $tring." is Village idiot." ], 5 );
  261.                     }
  262.                     elsif($tringt =~ /Angel/i) {
  263.                         my $alarm_id = $irc->delay( [ privmsg => $tring => "I'm seer, and you're angel, right? Protect me, I'll keep looking for the wolf." ], 2 );
  264.                     }
  265.                     elsif($tringt =~ /Doppleganger/i) {
  266.                         my $alarm_id = $irc->delay( [ privmsg => $tring => "Dopple me for seer!" ], 2 );
  267.                     }
  268.                 }
  269.                 elsif($UserMsg =~ /(.*?) targetted (.*?)!/i) {
  270.                     $tring = $1;
  271.                     $tringt = $3;
  272.                     my $alarm_id = $irc->delay( [ privmsg => "#WWolf" => $tring." targetted ".$tringt."." ], 5 );
  273.                     $WW{"Vote"} = $tring;
  274.                 }
  275.                 elsif($UserMsg =~ /The (wolves|other wolves) are: (.*)/i) {
  276.                     @WWolfs = split /, /, $1;
  277.                     print "Updating players...\n";
  278.                     foreach (@WWolfs) {
  279.                         $WW{"Players"} =~ s/$_ //gs;
  280.                         @WWPlayers = split / /, $WW{"Players"};
  281.                         print "Updating players...\n".@WWPlayers."\n";
  282.                     }
  283.                 }
  284.             }
  285.         }
  286.     }
  287.     if($Event =~ /(CHANMSG|NOTICE|PRIVMSG)/) {
  288.         #####################
  289.         #   Owner commands  #
  290.         #####################
  291.         if(($UserMsg =~ /^!End$/i) && ($UserNick =~ /^$BotOwner$/i)) {
  292.             $irc->yield(quit => POE::Component::IRC::Common::MAGENTA."~ Goodbye ~");
  293.         }
  294.         elsif($_[4] =~ /^!Do (.*?) (.*?) (.*)/i) {
  295.             @Temp = ($1,$2,$3);
  296.             if($UserNick =~ /^$BotOwner$/i) {
  297.                 $irc->yield($Temp[0] => $Temp[1] => $Temp[2]);
  298.             }
  299.         }
  300.         elsif($UserMsg =~ /^!Nick (.*)/i) {
  301.             $tring = $1;
  302.             if($UserNick =~ /^$BotOwner$/i) {
  303.                 $BotNick = $tring;
  304.                 $irc->yield(nick => $tring);
  305.             }
  306.         }
  307.         elsif($UserMsg =~ /^!Amsg (.*)/i) {
  308.             $tring = $1;
  309.             if($UserNick =~ /^$BotOwner$/i) {
  310.                 $irc->yield(privmsg => $BotChannels => $tring);
  311.             }
  312.         }
  313.         #####################
  314.         #Important Commands #
  315.         #####################
  316.         elsif($UserMsg =~ /^!(Help|Commands)$/i) {
  317.             Command($Com."Bot made by Caaz");
  318.         }
  319.         elsif($UserMsg =~ /^!reload$/i) {
  320.             open DATA, "<".$BotPath."DBUsers.txt";
  321.             @DB = <DATA>;
  322.             chomp($DBUsers = $DB[0]);
  323.             close DATA;
  324.         }
  325.         elsif($UserMsg =~ /^!AJ$/i) {
  326.             open DATA, "<".$BotPath.(split / /, $Connect)[0].(split / /, $Connect)[1]."-AutoJoin.txt";
  327.             @Channels=<DATA>;
  328.             close DATA;
  329.             if($Channels[0] !~ /$Where,/i) {
  330.                 open DATA, ">>".$BotPath.(split / /, $Connect)[0].(split / /, $Connect)[1]."-AutoJoin.txt"; print DATA "$Where,"; close DATA;
  331.                 Command($Com."Channel added to AutoJoin.");
  332.             }
  333.             else {
  334.                 Command($Com."Channel already in AutoJoin.");
  335.             }
  336.         }
  337.         #####################
  338.         #  Random Commands  #
  339.         #####################
  340.         elsif($UserMsg =~ /^!Alert$/i) {
  341.             print "\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\a\n";
  342.         }
  343.         #####################
  344.         #        END        #
  345.         #####################
  346.     }
  347. }
  348. sub Regexit {
  349.     ($Reg = $_[0]) =~ s/\\/\\\\/gs;
  350.     $Reg =~ s/(\?|\(|\)|\||\[|\|\{|\}|\.)/\\$1/gs;
  351.     $Reg =~ s/\*/\(\.\*\)/gs;
  352.     return $Reg;
  353. }
  354. sub CREATEUser {
  355.     if($DBUsers !~ /(.*)<$_[0] :(.*?)>(.*)/i) {
  356.         open DATA, ">>".$BotPath."DBUsers.txt";
  357.         print DATA "<".$_[0]." : \\\[Access 1\\\] >";
  358.         close DATA;
  359.         open DATA, "<".$BotPath."DBUsers.txt";
  360.         @DB = <DATA>;
  361.         chomp($DBUsers = $DB[0]);
  362.         close DATA;
  363.     }
  364. }
  365. sub SpamProtect {
  366.     my($Sec, $Min, $Hour, $Day, $mon, $year, $WDay, $YDay) = localtime;
  367.     return (((($YDay * 24) * 60) * 60) + (($Hour * 60) * 60) + ($Min * 60) + $Sec);
  368. }
  369. sub Command {
  370.     $PrivmsgA = $_[0];
  371.     @PrivmsgB = split / /, $PrivmsgA;
  372.     $ComA = shift(@PrivmsgB);
  373.     $ComB = shift(@PrivmsgB);
  374.     $BotFont = "";
  375.     $irc->yield($ComA => $ComB => POE::Component::IRC::Common::MAGENTA.(join " ", @PrivmsgB)."");
  376. }
  377. sub GetTime { # This is just for timestamps, just leave it.
  378.     my($Sec, $Min, $Hour, $Day, $Mon, $Year, $WDay, $YDay) = localtime;
  379.     my $APM = "AM";
  380.     if($Hour > 12) { $Hour -= 12; $APM = "PM"; }
  381.     if($Hour < 10) { $Hour = "0".$Hour; }
  382.     if($Min < 10) { $Min = "0".$Min; }
  383.     if($Sec < 10) { $Sec = "0".$Sec; }
  384.     return "[ $Hour:$Min:$Sec $APM ]    ";
  385. }
  386. # Caaz was here.
  387. $poe_kernel->run();
  388. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement