]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TwitchBot.php
fix reevaluate command class name
[alttp.git] / app / TwitchBot / TwitchBot.php
index 79b6bf13f1861af5bde63e400992cbc060e08ae0..63f4236f685e92bf659610257e93177a78cf7a11 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\TwitchBot;
 
 use App\Models\Channel;
+use App\Models\TwitchBotCommand;
 use App\Models\TwitchToken;
 use Monolog\Handler\StreamHandler;
 use Monolog\Logger;
@@ -22,6 +23,9 @@ class TwitchBot {
                if (!$this->token) {
                        throw new \Exception('unable to find access token');
                }
+               if ($this->token->hasExpired()) {
+                       $this->token->refresh();
+               }
 
                $this->connector = new Connector();
                $this->connect();
@@ -155,7 +159,7 @@ class TwitchBot {
                        if (!$this->ready) return;
                        if (time() - $this->last_contact < 60) return;
                        try {
-                               $this->sendIRCMessage(IRCMessage::ping());
+                               $this->sendIRCMessage(IRCMessage::ping($this->nick));
                        } catch (\Exception $e) {
                        }
                });
@@ -169,6 +173,20 @@ class TwitchBot {
        }
 
 
+       protected function listenCommands() {
+               $this->getLoop()->addPeriodicTimer(1, function () {
+                       if (!$this->isReady()) return;
+                       $command = TwitchBotCommand::where('bot_nick', '=', $this->nick)->where('status', '=', 'pending')->oldest()->first();
+                       if ($command) {
+                               try {
+                                       $command->execute($this);
+                               } catch (\Exception $e) {
+                               }
+                       }
+               });
+       }
+
+
        private $logger;
 
        private $nick;