PHP IRC Bot Undefined Offset -
PHP IRC Bot Undefined Offset -
okay. i'm building irc bot in php. i'm noob php, bare me if it's stupid mistake. error gives me lines 45 & 50 error "undefined offset: 4 in /*//*/grue.php on line 50"
here's lines: line 45: $command = str_replace(array(chr(10), chr(13)), '', $this -> ex[3]);
line 50: switch($this -> ex[4]) {
here's rest of code:
<?php //so bot doesn't stop. set_time_limit(0); ini_set('display_errors', 'on'); //sample connection. $config = array('server' => 'irc.foonetic.net', 'port' => 6667, 'channel' => '#lingubender', 'name' => 'kibot', 'nick' => 'grue', 'pass' => '',); class grue { var $socket; // tcp/ip connection var $ex = array(); // messages function __construct($config) { $this -> socket = fsockopen($config['server'], $config['port']); $this -> login($config); $this -> main($config); } /* log server @param array */ function login($config) { $this -> write_data('user', $config['nick'].' :'.$config['name']); $this -> write_data('nick', $config['nick']); $this -> enter_channel($config['channel']); } //* grabs/displays info function main($config) { $data = fgets($this -> socket, 256); echo nl2br($data); flush(); $this -> ex = explode(' ', $data); if ($this -> ex[0] == 'ping') { write_data('pong', $this -> ex[1]); } $command = str_replace(array(chr(10), chr(13)), '', $this -> ex[3]); strtolower($command); if ($command == ':grue' || ':gu') { switch($this -> ex[4]) { case 'join': enter_channel($this -> ex[5]); break; case 'part': $this -> write_data('part'.$this -> ex[5].' :', $this -> ex[6]); break; case 'repeat': $message = ""; ($i = 5; $i <= (count($this -> ex)); $i++) { $message .= $this -> ex[$i]." "; } $this -> write_data('privmsg '.$this -> ex[4].' :', $message); break; case 'restart': echo "<meta http-equiv=\"refresh\" content=\"5\">"; exit; case 'shutdown': $this -> write_data('quit', $this -> ex[5]); exit; } } $this -> main($config); } function write_data($cmd, $msg = null) { if ($msg == null) { fputs($this -> socket, $cmd."\r\n"); echo '<strong>'.$cmd.'</strong><br>'; } else { echo '<strong>'.$cmd.' '.$msg.'</strong><br>'; } } function enter_channel($channel) { if (is_array($channel)) { foreach ($channel $chan) { $this -> write_data('join', $chan); } } else { $this -> write_data('join', $channel); } } } $bot = new grue($config); ?>
i've checked brackets, , parenthesis; think of didn't work. oh, it helps, when ran it played on above errors(45 & 50) 60 times.
thank help in advance.
well, see several problems in code, i'll seek address them , offer solutions:
you using recoursive functions. main
method should include while loop:
while (!feof($this->socket)) { ... ... ... }
this way script end when socket connection dies.
your ping pong fail. calling method without $this->
.
you're not lowering command's case. need assign strtolower($command)
($command = strtolower($command);
maybe?)
your status true. if ($command == ":grue" || ":gu")
":gu"
true. if ($command == ":grue" || $command == ":gu")
maybe?
other that, should have each command method.
as error, seek var_dump($this->ex)
when error occurs, print print_r(debug_backtrace())
, see function called when error occurred.
php undefined offset bots
Comments
Post a Comment