Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
命令电报机器人问题(php)_Php_Telegram Bot_Php Telegram Bot - Fatal编程技术网

命令电报机器人问题(php)

命令电报机器人问题(php),php,telegram-bot,php-telegram-bot,Php,Telegram Bot,Php Telegram Bot,我正在用php语言制作一个电报机器人,我想给一个命令添加一个冷却时间,我已经用sleep()尝试过了,但是结果还是一样,它不起作用,有人能帮我吗? 至少这是可能的?或者我需要用另一种语言重新编写bot 代码如下: <?php namespace Longman\TelegramBot\Commands\UserCommands; use Longman\TelegramBot\Commands\UserCommand; use Longman\TelegramBot\Reque

我正在用php语言制作一个电报机器人,我想给一个命令添加一个冷却时间,我已经用sleep()尝试过了,但是结果还是一样,它不起作用,有人能帮我吗? 至少这是可能的?或者我需要用另一种语言重新编写bot

代码如下:

    <?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;

class DiscordCommand extends UserCommand{
    protected $name = 'discord';
    protected $description = 'Linka server discord';
    protected $usage = '/discord';
    protected $version = '1.0.0';

    public function execute()
    {
        $message = $this->getMessage();
        $chat_id = $message->getChat()->getId();
        $message_id = $message->getMessageId();
        $text = 'Ciaoo';
        $data = [
            'chat_id' => $chat_id,
            'text' => $text];

            $started_at = time();
            if($current_time==$started_at)
                return Request::sendMessage($data);
            $cooldown = 60*60*1; //1 minutes
            $current_time = time();
            $timeLeft = $current_time - $started_at;
            if($timeLeft >= $cooldown)
                return Request::sendMessage($data);

我将对您的代码进行注释,我在这里看到一些可能的错误:

        $started_at = time();
        if($current_time==$started_at) // You're using $current_time, but is not set 
            return Request::sendMessage($data);
        $cooldown = 60*60*1; //This is not 1 minutes, 60*60*1 is 1 hour
        $current_time = time();
        $timeLeft = $current_time - $started_at;
        if($timeLeft >= $cooldown)
            return Request::sendMessage($data);

我将对您的代码进行注释,我在这里看到一些可能的错误:

        $started_at = time();
        if($current_time==$started_at) // You're using $current_time, but is not set 
            return Request::sendMessage($data);
        $cooldown = 60*60*1; //This is not 1 minutes, 60*60*1 is 1 hour
        $current_time = time();
        $timeLeft = $current_time - $started_at;
        if($timeLeft >= $cooldown)
            return Request::sendMessage($data);

你能分享你所尝试过的,以便我们能更好地帮助你吗?当然没问题,我可以分享代码,我知道在返回代码结束后,我想知道是否有办法进行冷却。你能分享你所尝试过的,以便我们能更好地帮助你吗?当然没问题,我可以分享代码,我知道在返回代码结束后,我想知道是否有办法进行冷却。好的,我确信这部分不好,应该如何设置$current\u time?(很抱歉,我不太熟悉php编码)我们不知道您是否在代码的另一部分中设置了$current_time,但在您的示例中,您在设置之前使用了该变量。尝试移动
$current_time=time()
if($current\u time==$started\u at)
之前,问题是
if
将始终返回true,因为两个变量($current\u time和$started\u at)是相同的。我尝试移动$current\u time,但相同的结果不起作用,我希望命令给出输出,然后用户需要等待再次使用它,也许有其他的方法可以做到这一点,我肯定这部分不好,怎么设置$current_time?(很抱歉,我不太熟悉php编码)我们不知道您是否在代码的另一部分中设置了$current_time,但在您的示例中,您在设置之前使用了该变量。尝试移动
$current_time=time()
if($current\u time==$started\u at)
之前,问题是
if
将始终返回true,因为两个变量($current\u time和$started\u at)是相同的。我尝试移动$current\u time,但相同的结果不起作用,我希望命令给出输出,然后用户需要等待再次使用它,也许还有别的办法