PHP IRC Bot 未定义偏移量

发布于 2024-12-29 09:41:15 字数 2998 浏览 2 评论 0原文

我正在用 PHP 构建一个 IRC 机器人。它给我的错误是第 45 行和第 45 行。 50 并出现错误“未定义偏移:4 in /****///Grue.php on line 50”

以下是这些行: 第 45 行:$command = str_replace(array(chr(10), chr(13)), '', $this -> ex[3]);

第 50 行:switch( $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);
?>

我已经检查了所有的括号和圆括号;我能想到的一切都不起作用。如果有帮助的话,当我运行它时,它会播放上述错误(45 和 50)大约 60 次。

I'm building a IRC bot in PHP. The error it gives me is for lines 45 & 50 with the error "Undefined offset: 4 in /****///Grue.php on line 50"

Here's those lines:
Line 45: $command = str_replace(array(chr(10), chr(13)), '', $this -> ex[3]);

Line 50: switch($this -> ex[4]) {

Here's he rest of the code:

<?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);
?>

I've checked all the brackets, and parenthesis; Everything I could think of didn't work. If it helps, when I ran it it played over the above errors (45 & 50) about 60 times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2025-01-05 09:41:15

好吧,我在您的代码中发现了几个问题,我将尝试解决它们并提供解决方案:

  • 您正在使用递归函数。 main 方法应包含 while 循环:

    while (!feof($this->socket)) {
    
        ...
        ...
        ...
    
    }
    

    这样,当套接字连接断开时,脚本就会结束。

  • 你的乒乓球将会失败。您正在调用没有 $this-> 的方法。

  • 您没有降低命令的大小写。您需要将 strtolower($command) 分配给某些内容($command = strtolower($command); 也许?)

  • 您的条件始终为真。 if ($command == ":grue" || ":gu") ":gu" 始终为 true。 if ($command == ":grue" || $command == ":gu") 也许?

除此之外,每个命令都应该有一个方法。

至于错误,尝试var_dump($this->ex),当错误发生时,也打印一个print_r(debug_backtrace()),看看到底是什么函数发生错误时被调用。

Well, I see several problems in your code, I'll try to address them and offer solutions:

  • You are using recoursive functions. The main method should include a while loop:

    while (!feof($this->socket)) {
    
        ...
        ...
        ...
    
    }
    

    This way the script will end when the socket connection dies.

  • Your PING PONG will fail. You are calling the method without $this->.

  • You're not lowering the command's case. You need to assign strtolower($command) to something ($command = strtolower($command); maybe?)

  • Your condition is always true. if ($command == ":grue" || ":gu") ":gu" is always true. if ($command == ":grue" || $command == ":gu") maybe?

Other than that, you should have each command with a method.

As for the error, try to var_dump($this->ex) when the error occurs, also print a print_r(debug_backtrace()), to see exactly what function was called when the error occurred.

停滞 2025-01-05 09:41:15

首先:

if ($command == ':grue' || ':gu') {

应该是:

if ($command == ':grue' || $command == ':gu') {

对于偏移错误,您应该首先检查其是否设置:

if (isset($this -> ex[4]))

First of all this:

if ($command == ':grue' || ':gu') {

should be:

if ($command == ':grue' || $command == ':gu') {

and for the offset error you should be checking if its set first:

if (isset($this -> ex[4]))
泅人 2025-01-05 09:41:15

该错误消息表明您正在尝试获取数组中不存在的元素;例如,您有一个包含 3 个元素的数组,并且您正在尝试获取第 5 个元素。

在您的具体情况下,由于 $this->ex 是使用explode生成的,您可以说分解的文本没有您期望的部分数量。我建议您在爆炸后使用 var_dump($this->ex) ,这样您就可以了解文本是如何分离的以及为什么它不是您所期望的。

That error message indicates you're trying to get an element on the array that doesn't exist; for example, you have an array with 3 elements and you're trying to get the 5th element.

In your specific case, as $this->ex is generated by using explode, you can say the exploded text doesn't have the quantity of parts you are expecting. I'd suggest you use var_dump($this->ex) after doing the explode, so you can get an idea of how is your text being separated and why is it not what you expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文