Jump to content

Recommended Posts

hey guys, below is my code for my IRC robot, there is no network lag as it is running locally, but the bot takes AGES to reply to anything I say, i think the php interpreter is running slow perhaps due to some bad code, but i just cant figure out what it is! thanks!

 

<?php
include("functions.php");
error_reporting(0);
include("classes.php");
$timer = false;
$amiready = false;
/*if(!details()){
$display = new interfacer;
$display->the_interface();
}
else{*/
//$varset = new variablesetter();
//$varset->setvars();
$channel = "#bots";
$tyu = 1;

$sock = fsockopen("localhost", 6667);
fputs($sock,"USER PHPBot thegeeks.us PHP :PHP Bot\n");

//Sends the NICK to server
fputs($sock,"NICK Anetslek\n");
$currentnick = "Anetslek";
//Join #lamerchan
say("sup", $channel, $sock);
$loghandle = fopen(date(d)."-".date(m)."-".date(Y).".txt", "a+");
logw($loghandle, "\r\n----------------------------\r\nSTARTING LOGGING AT ". date(H).":".date(i).":".date(s)."\r\n----------------------------\r\n\r\n");
sqlcon();
while (1) {

   //Recieves the data into $data in 128 bytes.
   while ($data = fgets($sock,128)) {
      //puts the data in an array by word
      $get = explode(' ', $data);
      //echo $data;
fputs($sock,"JOIN #bots\r\n");

$timer = true;
      //Server Pinged us lets reply!
      if ($get[0] == "PING") {
      //echo "\nSending pong ".$get[1]."\n";
	fputs ($sock, "PONG ".$get[1]."\n");
      }
      //////////////BOT FUNCTIONS HERE
  
      $speech1 = explode(":", $data);
      $command12 = $speech1[2];
      $command123 = explode(" ", $command12, 2);
      $command1 = $command123[0];
  
  $username1 = explode("!", $data);
  $username2 = explode(":", $username1[0]);
  $username = $username2[1];
  
      $secondcommand12 = $command123[1];      
      $secondcommand2 = str_replace("\n", "", $secondcommand12);
      $secondcommand = str_replace("\r", "", $secondcommand2);
      $args12 = $command123[1];
  
      $args1 = str_replace("\n", "", $args12);
      $args = str_replace("\r", "", $args1);
  
      $command2 = str_replace("\n", "", $command1);
      $command = str_replace("\r", "", $command2);
  
  if(strstr($data, "PRIVMSG")){
  logw($loghandle, $username." : ".$command." ".$args."\r\n");
  if($command == "Speak"){
  echo 'received this at'.date(i);
  while(say("Debug me please mejt!", $channel, $sock)){
  echo 'echoing lol';
  }
  echo 'received this at'.date(i);
  }
  //log the words and phrases
  /*if($command[0] != "#"){
  
  $phrase = $command." ".$args;
  if($phrase != "You have not registered"){
  //sqlcon();
  //check its not there already!
      $sqlq = mysql_query("SELECT * FROM `phrases` WHERE `Phrase` = '$phrase'");
  $rose = mysql_num_rows($sqlq);
  if($rose == 0){
  mysql_query("INSERT INTO  `phrases` (
`ID` ,
`Phrase` ,
`Type` ,
`Replies`
)
VALUES (
NULL ,  '$phrase',  'SetMe',  'SetMe'
);");
}
  }
  }*/
  }
      //echo $user." says: ".$command."\n";
  
  echo $username." says: ".$command." ".$args."\n";  
      
      if($command == "Reboot"){
      echo "Rebooting, brb!\n";
      
      say("Back in a bit, boys!", $channel, $sock);
      fputs($sock, "QUIT :Rebooting\n");
      fclose($sock);
      
      exec("main.exe");
      exec("C:\\Documents\ and\ Settings\\Administrator\\Desktop\\BIRCD\\restart.bat");
      die("Had to quit.");
      }
  
  
  if($command == "#phrases"){
  //format time nicely
  say("I'd rather like to learn some new responses, can you help me?", $channel, $sock);
  //get some phrases
  sqlcon();
  $phq = mysql_query("SELECT * FROM `phrases` WHERE `Replies` = 'SetMe' LIMIT 20");
  $random_sel = rand(0, mysql_num_rows($phq));
  //print into an array
  $xy = 0;
  while($arrey = mysql_fetch_array($phq)){
  $xy++;
  $phrase_array[$xy] = $arrey['Phrase'];
  }
  say("Can you tell me a good reply to use when someone says \"".$phrase_array[$random_sel]."\" please?", $channel, $sock);
  $lastphrase = $phrase_array[$random_sel];
  say("Say \"#respond <answer>\" to set a response for the above phrase!", $channel, $sock);
      }
      
  if($command == "#respond"){
  sqlcon();
  echo $lastphrase;
  mysql_query("UPDATE `phrases` SET `Replies` = '$args' WHERE `Phrase` = '$lastphrase'");
  }
  
      if($command == "#calc"){
      $commando1 = explode(" ", $command);
      $commando = $secondcommand;
  
  if(strstr($commando1[1], "c ans")){
  say("Unsetting \"ans\"", $channel, $sock);
  unset($ans);
  }
  
      if(strstr($commando, "+")){
      $numbers = explode("+", $secondcommand);
      $one = $numbers[0];
      $two = $numbers[1];
  
  if($one == "ans"){
  $one = $ans;
  }
  
  if($two == "ans"){
  $two = $ans;
  }
  
      $three = $one+$two;
      say("ans=".$three, $channel, $sock);
  $ans = $three;	  
      }
      
      if(strstr($commando, "-")){
      $numbers = explode("-", $secondcommand);
      $one = $numbers[0];
      $two = $numbers[1];  
  if($one == "ans"){
  $one = $ans;
  }
  
  if($two == "ans"){
  $two = $ans;
  }
      $three = $one-$two;
      say("ans=".$three, $channel, $sock);
  $ans = $three;	  
      }
      
      if(strstr($commando, "*")){
      $numbers = explode("*", $secondcommand);
      $one = $numbers[0];
      $two = $numbers[1];
    if($one == "ans"){
  $one = $ans;
  }
  
  if($two == "ans"){
  $two = $ans;
  }
      $three = $one*$two;
      say("ans=".$three, $channel, $sock);
  $ans = $three;
      }
      
      if(strstr($commando, "/")){
      $numbers = explode("/", $secondcommand);
      $one = $numbers[0];
      $two = $numbers[1];
    if($one == "ans"){
  $one = $ans;
  }
  
  if($two == "ans"){
  $two = $ans;
  }
      $three = $one/$two;
      say("ans=".$three, $channel, $sock);
  $ans = $three;
      }
  }
      
      if($command == "#reboot"){
      echo "Rebooting the realm!\n";
      
      say("See you on the flip side.", $channel, $sock);
      fclose($sock);
      exec("main.exe");
      die("Had to quit.");
      }
            
      if($command == "#quit"){
      echo "Quitcommand!\n";
      
      say("Goodbye!", $channel, $sock);
      fputs($sock, "QUIT :".$args."\n");
      fclose($sock);
  logw($loghandle, "\r\n-------------------------\r\nLOGGING ENDED AT ".date(H).":".date(i).":".date(s)."\r\n-------------------------");
  fclose($loghandle);
      die("Had to quit.");
      }
  	 
     if($command == "#clearlogs"){
 logw($loghandle, "Clearing logs from ".$args);
 fclose($loghandle);
 error_reporting(1);
 $loghandle1 = fopen($args.".txt", "w+");
 fclose($loghandle1);
 unlink($args.".txt");
 $loghandle = fopen(date(d)."-".date(m)."-".date(Y).".txt", "a+");
 }

 if($command == "#name"){
 say("Changing my name!", $channel, $sock);
 logw($loghandle, "Changing my name from ".$currentnick." to ".$args);  
 $currentnick = $args;
 raw("NICK :".$args, $sock);
 }

 if($command == "#md5"){
 $con = mysql_connect("localhost", "root", "");
 mysql_select_db("bot");
 $checkhash = mysql_query("SELECT `hash` FROM `$comm` WHERE `raw` = '$args'");
 $checkhashrows = mysql_num_rows($checkhash);
 if($checkhashrows == 0){
 logw($loghandle, strtoupper($comm)." hashing ".$args);
 $hash = md5($args);
 mysql_query("INSERT INTO `bot`.`$comm` (`ID`, `raw`, `hash`) VALUES (NULL, '$args', '$hash');");
 say(strtoupper($comm)." hash for ". $args." : ".$hash, $channel, $sock);
 }
 else{
 $row = mysql_fetch_array($checkhash);
 say(strtoupper($comm)." hash for ". $args." : ".$row['hash'], $channel, $sock);
 }
 mysql_close($con);
 }

 if($command == "#crackmd5"){
 $con = mysql_connect("localhost", "root", "");
 mysql_select_db("bot");
 $checkhash = mysql_query("SELECT `raw` FROM `$comm` WHERE `hash` = '$args'");
 $checkhashrows = mysql_num_rows($checkhash);
 if($checkhashrows == 0){
 say("That's not in the database, sorry!", $channel, $sock);
 }
 else{
 $row = mysql_fetch_array($checkhash);
 say("Raw string for hash ". $args." : ".$row['raw'], $channel, $sock);
 }
 }
      }
      }
//} 
?>

Link to comment
https://forums.phpfreaks.com/topic/183111-php-bot-lag/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.