Advertisement
Guest User

Untitled

a guest
May 21st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.98 KB | None | 0 0
  1. package PipSqueek::Plugin::Freezer;
  2. use base qw(PipSqueek::Plugin);
  3. use DBI;
  4. use DBD::mysql;
  5. use Time::localtime;
  6.  
  7. sub plugin_initialize {
  8.     my $self = shift;
  9.  
  10.     # CONFIG VARIABLES
  11.     my $platform = "mysql";
  12.     my $database = "orbiter_radio";
  13.     my $host = "localhost";
  14.     my $port = "3308";
  15.     my $tablename = "freezer";
  16.     my $user = "Radiobot";
  17.     my $pass = "Yeah, oops";
  18.    
  19.     # set the data source name
  20.     my $dsn = "dbi:$platform:$database:$host:$port";
  21.  
  22.     # now connect and get a database handle
  23.     $dbh = DBI->connect($dsn, $user, $pass)
  24.             or die $DBI::errstr;
  25.    
  26.     $Penguin[0] = "Have you ever wrestled a penguin? No? Then you go try that.";
  27.     $Penguin[1] = "The Penguin Resists!";
  28.     $Penguin[2] = "Uhh... I don't think he wants to move right now.";
  29.     $Penguin[3] = "Yeah, he's WAY too busy fapping for that.";
  30.     $Penguin[4] = "Not a chance in hell. He'd just try to buttfuck me or something.";
  31.     $Penguin[5] = "Have you ever wrestled a penguin? No? Then you go try that.";
  32.     $Penguin[6] = "I'm... I'm scared he'll hurt me";
  33.    
  34.     $self->plugin_handlers({
  35.         'multi_freezer'     =>  'freezer', # list items in freezer. Needs limiting to avoid floods.
  36.         'multi_freezer+'    =>  'freezer_add', # add item to freezer. May need an item-exists check.
  37.         'multi_freezer-'    =>  'freezer_rem', # remove item from freezer. Needs to be checked against "removable" variable for the "You can't remove the penguin" jokes.
  38.     });
  39. }
  40.  
  41. sub freezer {
  42.     my ($self, $message) = @_;
  43.    
  44.     # Find the items we're interested in, who added them, and when
  45.     my $query = 'SELECT * FROM freezer';
  46.     my $sth = $dbh->prepare($query);
  47.     # run the query
  48.     $sth->execute()
  49.     or die "Fail."; # REALLY informative error :P
  50.    
  51.     # format and display query result. Not currently checked against the 40 limit - i.e if there are more than forty the bot won't tell you.
  52.     $self->respond($message, "Items in Xyon's freezer at the moment include:");
  53.     while(@row = $sth->fetchrow_array()) {
  54.         $self->respond($message, "$row[1]: $row[2] (added by $row[3])");  
  55.     }
  56.     return $self->respond($message, "End of list.");
  57. }
  58.  
  59. sub freezer_add {
  60.    
  61.     my ($self,$message)=@_;
  62.     my $item = $message->command_input(); # Takes the item they want to add.
  63.     my $nick = $message->{nick}; # For the "who added this" data
  64.    
  65.     my $tm = localtime; # Get the date and time. Not sure whose timezone this works in atm >.<
  66.     my ($DAY, $MONTH, $YEAR, $HOUR, $MINUTE, $SECOND) = ($tm->mday, $tm->mon+1, $tm->year+1900, $tm->hour, $tm->min, $tm->sec);
  67.    
  68.     # list number sorting
  69.    
  70.     my $query = 'SELECT listnum FROM freezer ORDER BY listnum desc limit 1';
  71.     my $sth = $dbh->prepare($query);
  72.     # A bit of a hackjob, but it works...
  73.     $sth->execute();
  74.     my $listnumtemp = $sth->fetchrow();
  75.     my $listnum = ($listnumtemp)+1;
  76.  
  77.     #First, make sure they gave us an item to add
  78.    
  79.     if ($item eq "") {
  80.         return $self->respond($message, "You have to actually give me an item to add to the freezer, dummy.");
  81.     }
  82.    
  83.     if ($item eq "freezer" || $item eq "Freezer") {
  84.         return $self->respond($message, "Sorry, but you don't have the authority to buttfuck physics like that, $nick");
  85.     }
  86.     # If they did, make sure the freezer isn't full.
  87.    
  88.     my $check = 'SELECT * FROM freezer WHERE listnum > 7';
  89.     my $sthcheck = $dbh->prepare($check);
  90.     # Run the query
  91.     $sthcheck->execute();
  92.     # A bit of a hackjob, but it works...
  93.     my $freezerfull = $sthcheck->fetchrow();
  94.    
  95.     if ($freezerfull != NULL)
  96.     {
  97.         return $self->respond($message, "Sorry, the freezer is full!"); # Can't add !
  98.     }
  99.    
  100.     # freezer isn't full, carry on
  101.     else {
  102.         my $query = 'INSERT INTO freezer(listnum, item_name, added_by, added_on, removable) values (?,?,?,?,?)';
  103.         my $sth = $dbh->prepare($query) or die "WTF $DBI::errstr;";
  104.    
  105.         $sth->execute("$listnum", "$item", "$nick", "$YEAR-$MONTH-$DAY $HOUR:$MINUTE:$SECOND", 1)
  106.         or die "Failed to access database properly. Prod Xyon until he fixes it. $DBI::errstr\n";
  107.    
  108.         return $self->respond($message, "Item '$item' added to freezer."); # confirm.
  109.     }
  110. }
  111.  
  112. sub freezer_rem {
  113.     my ($self,$message)=@_;
  114.     my $item = $message->command_input(); # Takes the item they want to remove.
  115.    
  116.     # firstly, check for the penguin!
  117.    
  118.     if ($item eq "1")
  119.     {
  120.         my $range = 6;
  121.  
  122.         my $num = int(rand($range));
  123.         return $self->respond($message, $Penguin[$num]);
  124.     }
  125.     if ($item eq "2")
  126.     {
  127.         return $self->respond($message, "The Penguin reminds you that removing his fappage material is unwise.");
  128.     }
  129.    
  130.     if ($item eq "") {
  131.         return $self->respond($message, "Take it out? Take what out? WHAT?");
  132.     }
  133.    
  134.    
  135.     my $query = 'SELECT * FROM freezer WHERE listnum = ?';
  136.     my $sth = $dbh->prepare($query);
  137.    
  138.     $sth->execute($item);
  139.     my $itemexists = $sth->fetchrow();
  140.    
  141.     if ($itemexists eq 'NULL') {
  142.         return $self->respond($message, "Sorry, I can't find that in the freezer.");
  143.     }
  144.    
  145.     else {
  146.    
  147.         my $deljob = 'DELETE FROM freezer WHERE listnum = ?';
  148.         my $sth1 = $dbh->prepare($deljob);
  149.        
  150.         $sth1->execute($item) or die "Query fail: $DBI::errstr;";
  151.        
  152.         return $self->respond($message, "Item removed from freezer.");
  153.     }
  154.    
  155. }
  156.  
  157. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement