X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;ds=sidebyside;f=app%2FTwitchBot%2FTwitchBot.php;h=c8d77a36d6f656007a0b3b50c009689b55f3adbe;hb=8fd3830c342ed7734280407b03cc7ced6e116b10;hp=3cba5d958bac4975fd2e3fdf93fd901208ffcc18;hpb=cde5d79cf2f09d61fa7b181cd3a1a19050a4aeb3;p=alttp.git diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index 3cba5d9..c8d77a3 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -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; @@ -24,6 +25,8 @@ class TwitchBot { $this->connector = new Connector(); $this->connect(); + $this->listenCommands(); + $this->startPinger(); } public function getLogger() { @@ -75,16 +78,17 @@ class TwitchBot { public function handleWsMessage(Message $message, WebSocket $ws) { $irc_messages = explode("\r\n", rtrim($message->getPayload(), "\r\n")); foreach ($irc_messages as $irc_message) { - $this->logger->debug('received IRC message '.$irc_message); + $this->logger->info('received IRC message '.$irc_message); $this->handleIRCMessage(IRCMessage::fromString($irc_message)); } } public function handleWsClose(int $op, string $reason) { + $this->ready = false; $this->logger->info('websocket connection closed: '.$reason.' ['.$op.']'); if (!$this->shutting_down) { - $this->logger->info('reconnecting in 10 seconds'); - Loop::addTimer(10, [$this, 'connect']); + $this->logger->info('reconnecting in 5 seconds'); + Loop::addTimer(5, [$this, 'connect']); } } @@ -94,14 +98,19 @@ class TwitchBot { public function handleIRCMessage(IRCMessage $msg) { - if ($msg->isPrivMsg()) { - $this->handlePrivMsg($msg); - return; - } + $this->last_contact = time(); if ($msg->isPing()) { $this->sendIRCMessage($msg->makePong()); return; } + if ($msg->isPong()) { + return; + } + $msg->log(); + if ($msg->isPrivMsg()) { + $this->handlePrivMsg($msg); + return; + } if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') { $this->logger->notice('login failed, refreshing access token'); $this->token->refresh(); @@ -111,13 +120,14 @@ class TwitchBot { if ($msg->command == '001') { // successful login $this->joinChannels(); + $this->ready = true; return; } } public function handlePrivMsg(IRCMessage $msg) { $target = $msg->getPrivMsgTarget(); - if ($target[0] != '#') return; + if ($target[0] != '#') return; // direct message $text = $msg->getText(); if ($text[0] != '!') return; $channel = Channel::firstWhere('twitch_chat', '=', $target); @@ -145,7 +155,7 @@ class TwitchBot { public function joinChannels() { $this->logger->info('joining channels'); - $channels = Channel::where('twitch_chat', '!=', '')->get(); + $channels = Channel::where('twitch_chat', '!=', '')->where('join', '=', true)->get(); $names = []; foreach ($channels as $channel) { $names[] = $channel->twitch_chat; @@ -156,10 +166,35 @@ class TwitchBot { } } + private function listenCommands() { + $this->getLoop()->addPeriodicTimer(1, function () { + if (!$this->ready) return; + $command = TwitchBotCommand::where('status', '=', 'pending')->oldest()->first(); + if ($command) { + try { + $command->execute($this); + } catch (\Exception $e) { + } + } + }); + } + + private function startPinger() { + $this->getLoop()->addPeriodicTimer(15, function () { + if (!$this->ready) return; + if (time() - $this->last_contact < 60) return; + try { + $this->sendIRCMessage(IRCMessage::ping()); + } catch (\Exception $e) { + } + }); + } + public function sendIRCMessage(IRCMessage $msg) { $irc_message = $msg->encode(); - $this->logger->debug('sending IRC message '.$irc_message); + $this->logger->info('sending IRC message '.$irc_message); $this->ws->send($irc_message); + $this->last_contact = time(); } @@ -169,8 +204,11 @@ class TwitchBot { private $connector; private $ws; + private $ready = false; private $shutting_down = false; + private $last_contact; + } ?>