PHP IRC Bot未定义的偏移量
我正在用PHP构建一个IRC机器人。它给我的错误是针对第45行和第50行的,错误为“第50行的未定义偏移量:4 in/******///Grue.php” 下面是这些台词: 第45行:PHP IRC Bot未定义的偏移量,php,undefined,offset,bots,Php,Undefined,Offset,Bots,我正在用PHP构建一个IRC机器人。它给我的错误是针对第45行和第50行的,错误为“第50行的未定义偏移量:4 in/******///Grue.php” 下面是这些台词: 第45行:$command=str_replace(数组(chr(10),chr(13)),“”,$this->ex[3]) 第50行:开关($this->ex[4]){ 以下是代码的其余部分: <?php //So the bot doesn't stop. set_time_limit(0); ini_set('d
$command=str_replace(数组(chr(10),chr(13)),“”,$this->ex[3])代码>
第50行:开关($this->ex[4]){
以下是代码的其余部分:
<?php
//So the 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 into 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 data
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 = "";
for ($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 as $chan) {
$this -> write_data('JOIN', $chan);
}
} else {
$this -> write_data('JOIN', $channel);
}
}
}
$bot = new Grue($config);
?>
好吧,我看到您的代码中有几个问题,我将尝试解决它们并提供解决方案:
- 您正在使用补偿函数。
main
方法应包括一个while循环:
while (!feof($this->socket)) {
...
...
...
}
这样,脚本将在套接字连接终止时结束
- 您的乒乓球将失败。您正在调用没有
$this->
的方法
- 您没有降低命令的大小写。您需要将
strtolower($command)
分配给某个对象($command=strtolower($command);
可能?)
- 你的条件总是正确的。
if($command==”:grue“| |“:gu”)
“:gu”
总是正确的。if($command==”:grue“| |$command==”:gu”)
可能吗
除此之外,每个命令都应该有一个方法
至于错误,请在错误发生时尝试var\u dump($this->ex)
,同时打印print\r(debug\u backtrace())
,以查看错误发生时调用了什么函数。首先:
if ($command == ':grue' || ':gu') {
应该是:
if ($command == ':grue' || $command == ':gu') {
对于偏移误差,您应该首先检查其是否已设置:
if (isset($this -> ex[4]))
该错误消息表示您试图获取数组中不存在的元素;例如,您有一个包含3个元素的数组,而您试图获取第5个元素
在您的特定情况下,由于$this->ex
是使用explode生成的,您可以说分解的文本没有您期望的部分数量。我建议您使用var\u dump($this->ex)
执行分解后,您可以了解文本是如何被分离的,以及为什么它不是您所期望的。这意味着在它运行的上下文中,$this->ex[4]
(或3)不存在,您试图访问一个不存在的数组元素,因此出现错误。您应该打印一个调试堆栈(print_r(debug_backtrace())
)并查看它的发展方向。此外,我有一个正在运行的PHP IRC bot,它比您的要复杂得多(每个用户都是一个对象,每个通道都是一个对象,良好的动态用户跟踪和通道监控,连接工作等等),如果您愿意,我可以与您共享。谢谢。在ex[3]执行上述命令之前,帮助我了解了它的情况。也谢谢。我不知道if(OR)语句有问题。