1

i'm making a Telegram bot using php language, i want to add a cooldown to a command, i already tried with sleep(), but the result still the same, it doesn't work, someone can help me? At least this is possible? or i need to re code the bot in another language?

here the code:

    <?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);
Lyuk
  • 11
  • 2

1 Answers1

0

I will comment your code, I see some possible errors here:

        $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);
Néstor
  • 416
  • 4
  • 10
  • ok, i was sure this part wasn't ok, how is supposed to be set $current_time? (sorry i'm new at coding in php) – Lyuk May 03 '20 at 11:09
  • We don't know if you are setting $current_time in another part of your code, but in your example you are using that variable before being set. Try to move `$current_time = time();` before your `if($current_time==$started_at)`, the problem is that your `if` will always return true, because both variables ($current_time and $started_at) are the same. – Néstor May 03 '20 at 11:44
  • I tried to move $current_time, but same result doesn't work, i want that the command give the output and then the user need to wait to use it again, maybe there are other way to do it – Lyuk May 03 '20 at 11:52